數(shù)據(jù)庫查詢語句

-- 查詢所有學(xué)生的信息
select * from tb_student;

-- 查詢所有老師的信息
select * from tb_teacher;

-- 查詢所有課程的信息
select * from tb_course;

-- 查詢所有課程名稱及學(xué)分(投影和別名)
select couname as 課程名稱, coucredit as 學(xué)分 from tb_course;

-- 查詢學(xué)生的姓名和性別
select stuname as 姓名, stusex as 性別 from tb_student;

select stuname as 姓名, case stusex when 1 then '男' else '女' end as 性別 from tb_student;

select stuname as 姓名, if(stusex, '男', '女') as 性別 from tb_student;

-- 查詢所有女學(xué)生的姓名和出生日期(篩選)
select stuname, stubirth from tb_student where stusex=0;

-- 查詢所有80后學(xué)生的姓名笨农、性別和出生日期(篩選)
select stuname, stusex, stubirth from tb_student where stubirth >= '1980-1-1' and stubirth <= '1989-12-31';

select stuname, stusex, stubirth from tb_student where stubirth between '1980-1-1' and '1989-12-31';

-- 查詢姓”楊“的學(xué)生姓名和性別(模糊)
select stuname, stusex from tb_student where stuname like '楊%';

-- 查詢姓”楊“名字兩個(gè)字的學(xué)生姓名和性別(模糊)
select stuname, stusex from tb_student where stuname like '楊_';

-- 查詢姓”楊“名字三個(gè)字的學(xué)生姓名和性別(模糊)
select stuname, stusex from tb_student where stuname like '楊__';

-- 查詢名字中有“不”字或“嫣”字的學(xué)生的姓名(模糊)
select stuname from tb_student where stuname like '%不%' or stuname like '%嫣%';

-- 查詢沒有錄入家庭住址的學(xué)生姓名(空值)
select stuname from tb_student where stuaddr is null;

-- 查詢錄入了家庭住址的學(xué)生姓名(空值)
select stuname from tb_student where stuaddr is not null;

-- 查詢學(xué)生選課的所有日期(去重)
select distinct seldate from tb_record;

-- 查詢學(xué)生的家庭住址(去重)
select distinct stuaddr from tb_student where stuaddr is not null;

-- 查詢男學(xué)生的姓名和生日按年齡從大到小排列(排序)
select stuname, stubirth from tb_student where stusex=1 order by stubirth asc, stuid desc;

select stuname as 姓名, datediff(curdate(), stubirth) div 365 as 年齡 from tb_student where stusex=1 order by 年齡 desc;

-- 查詢年齡最大的學(xué)生的出生日期(聚合函數(shù))
select min(stubirth) from tb_student;

-- 查詢年齡最小的學(xué)生的出生日期(聚合函數(shù))
select max(stubirth) from tb_student;

-- 聚合函數(shù) - max / min / sum / avg / count

-- 查詢學(xué)生總?cè)藬?shù)
select count(stuid) from tb_student;

-- 查詢男女學(xué)生的人數(shù)(分組和聚合函數(shù))
select if(stusex, '男', '女') as 性別, count(stuid) as 人數(shù) from tb_student group by stusex order by 人數(shù) desc;

-- 查詢課程編號(hào)為1111的課程的平均成績(jī)(篩選和聚合函數(shù))
select round(avg(score), 1) as 平均分 from tb_record where cid=1111;

-- 查詢學(xué)號(hào)為1001的學(xué)生所有課程的平均分(篩選和聚合函數(shù))
select round(avg(score), 1) as 平均分 from tb_record where sid=1001;

-- 查詢每個(gè)學(xué)生的學(xué)號(hào)和平均成績(jī)(分組和聚合函數(shù))
select sid as 學(xué)號(hào), round(avg(score), 1) as 平均分 from tb_record group by sid;

-- 查詢平均成績(jī)大于等于90分的學(xué)生的學(xué)號(hào)和平均成績(jī)
-- 注意:分組以前的篩選使用where子句 分組以后的篩選使用having子句
select sid as 學(xué)號(hào), round(avg(score), 1) as 平均分 from tb_record group by sid having 平均分>=90;

-- 查詢年齡最大的學(xué)生的姓名(子查詢)
-- 嵌套查詢:把一個(gè)查詢的查詢結(jié)果作為另外一個(gè)查詢的一部分來使用
select stuname from tb_student where stubirth=(
select min(stubirth) from tb_student
);

-- 查詢年齡最大的學(xué)生姓名和年齡(子查詢+運(yùn)算)
select stuname as 姓名, datediff(curdate(), stubirth) div 365 as 年齡 from tb_student where stubirth=(
select min(stubirth) from tb_student
);

-- 查詢選了兩門以上的課程的學(xué)生姓名(子查詢/分組條件/集合運(yùn)算)
select stuname from tb_student where stuid in (
select sid from tb_record
group by sid having count(sid)>2
);

-- 查詢學(xué)生姓名竭宰、課程名稱以及成績(jī)(連接查詢)
select stuname, couname, score from tb_student t1, tb_record t2, tb_course t3 where t1.stuid=t2.sid and t2.cid=t3.couid and score is not null;

select stuname, couname, score from tb_student inner join tb_record on stuid=sid inner join tb_course on couid=cid where score is not null;

-- 查詢選課學(xué)生的姓名和平均成績(jī)(子查詢和連接查詢)
select stuname, averagescore from tb_student as t1,
(select sid, avg(score) as averagescore from tb_record group by sid) as t2 where stuid=sid;

-- 查詢每個(gè)學(xué)生的姓名和選課數(shù)量(左外連接和子查詢)
select stuname, 選課數(shù)量 from tb_student,
(select sid, count(sid) as 選課數(shù)量 from tb_record group by sid) as t2 where stuid=sid;

-- 左外連接是指左邊的表里面不滿足條件的內(nèi)容如也給選出來
select stuname, ifnull(選課數(shù)量, 0) as 選課數(shù)量 from tb_student left outer join
(select sid, count(sid) as 選課數(shù)量 from tb_record group by sid) as t2 on stuid=sid limit 5 offset 3;

select stuname, ifnull(選課數(shù)量, 0) as 選課數(shù)量 from tb_student left outer join
(select sid, count(sid) as 選課數(shù)量 from tb_record group by sid) as t2 on stuid=sid limit 5, 3;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末切揭,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子廓旬,更是在濱河造成了極大的恐慌,老刑警劉巖涩盾,帶你破解...
    沈念sama閱讀 219,270評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件励背,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡址儒,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,489評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門莲趣,熙熙樓的掌柜王于貴愁眉苦臉地迎上來饱溢,“玉大人,你說我怎么就攤上這事÷唐福” “怎么了?”我有些...
    開封第一講書人閱讀 165,630評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵兽愤,是天一觀的道長挪圾。 經(jīng)常有香客問我,道長洼畅,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,906評(píng)論 1 295
  • 正文 為了忘掉前任帝簇,我火速辦了婚禮,結(jié)果婚禮上残揉,老公的妹妹穿的比我還像新娘芋浮。我一直安慰自己,他們只是感情好纸巷,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,928評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著何暇,像睡著了一般。 火紅的嫁衣襯著肌膚如雪裆站。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,718評(píng)論 1 305
  • 那天羽嫡,我揣著相機(jī)與錄音肩袍,去河邊找鬼。 笑死氛赐,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的滓侍。 我是一名探鬼主播牲芋,決...
    沈念sama閱讀 40,442評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼夕冲!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起歹鱼,我...
    開封第一講書人閱讀 39,345評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤卜高,失蹤者是張志新(化名)和其女友劉穎秩霍,沒想到半個(gè)月后蚁阳,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,802評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡颠悬,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,984評(píng)論 3 337
  • 正文 我和宋清朗相戀三年定血,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片灾票。...
    茶點(diǎn)故事閱讀 40,117評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡茫虽,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出濒析,到底是詐尸還是另有隱情,我是刑警寧澤号杏,帶...
    沈念sama閱讀 35,810評(píng)論 5 346
  • 正文 年R本政府宣布盾致,位于F島的核電站,受9級(jí)特大地震影響庭惜,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜蜈块,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,462評(píng)論 3 331
  • 文/蒙蒙 一百揭、第九天 我趴在偏房一處隱蔽的房頂上張望蜓席。 院中可真熱鬧器一,春花似錦厨内、人聲如沸渺贤。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,011評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽固棚。三九已至,卻和暖如春此洲,著一層夾襖步出監(jiān)牢的瞬間委粉,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,139評(píng)論 1 272
  • 我被黑心中介騙來泰國打工贾节, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人碰酝。 一個(gè)月前我還...
    沈念sama閱讀 48,377評(píng)論 3 373
  • 正文 我出身青樓戴差,卻偏偏與公主長得像,于是被迫代替她去往敵國和親暖释。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,060評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容