文章目錄
1.表一及相關題目
score表
sql
查詢class1班級英語平均分與class2中英語平均分的差值
SELECT score1-score2 差值 FROM (SELECT ROUND(AVG(`GRADE`),2) score1 FROM `score` WHERE `COURSE`='English' AND `CLASSNO`='Class1') avgsc1, (SELECT ROUND(AVG(`GRADE`),2) score2 FROM `score` WHERE `COURSE`='English' AND `CLASSNO`='Class2') avgsc2;
查詢各班級法語成績中低於班級法語平均分的人員個數
//建立只有法語成績的視圖 CREATE VIEW fren_score AS (SELECT * FROM score WHERE `COURSE`='French'); //根據fren_score視圖進行查詢 SELECT fren_score.`CLASSNO`,COUNT(1) FROM fren_score JOIN (SELECT `CLASSNO`,AVG(`GRADE`) avgf FROM fren_score GROUP BY `CLASSNO`) fren01 ON fren_score.`CLASSNO`=fren01.`CLASSNO` AND fren_score.`GRADE`<fren01.avgf GROUP BY fren_score.`CLASSNO`;
查詢每一個班級中英語成績最好的同窗,結果輸出:班級、姓名、分數
//建立只有英語成績的視圖 CREATE VIEW eng_score AS (SELECT * FROM `score` WHERE `COURSE`='English'); SELECT e1.`CLASSNO` '班級',e1.`NAME` '姓名',e1.`GRADE` '分數' FROM eng_score e1 LEFT JOIN eng_score e2 ON e1.`CLASSNO`=e2.`CLASSNO` AND e2.`GRADE`>= e1.`GRADE` GROUP BY e1.`STUDENTNO` HAVING COUNT(1)=1;
對全部班級的同窗英語成績進行從高往低的排序,結果輸出:排名 班級 姓名 分數
SELECT `CLASSNO` '班級',`NAME`'姓名',`GRADE` '分數',(@i:=@i+1) '排名' FROM eng_score,(SELECT @i:=0) i ORDER BY `GRADE` DESC;
2.表二及相關題目
score2表
bash
模塊1>=60&模塊2>=60 爲經過,屢次考試有一次經過爲經過,查詢結果顯示以下(輸出屢次成績中模塊一加模塊二成績最高的)
查詢結果
spa
//分別查詢經過並顯示最高成績和未經過顯示最高成績,並用鏈接 (SELECT sc2.`id` '考試編號',sc2.`name` '姓名',sc2.`score1` '模塊1考試成績',sc2.`score2` '模塊2考試成績', '考試經過' AS '考試結果' FROM (SELECT `name`,MAX(score1+score2) max_sc FROM score2 WHERE `score1`>=60 AND`score2`>=60 GROUP BY `name`) sc1 JOIN score2 sc2 ON sc1.`name`=sc2.`name` AND (sc1.max_sc=sc2.score1+sc2.score2) ) UNION (SELECT sc2.`id` '考試編號',sc2.`name` '姓名',sc2.`score1` '模塊1考試成績',sc2.`score2` '模塊2考試成績', '考試未經過' FROM (SELECT `name`,MAX(score1+score2) max_sc FROM score2 WHERE `name` NOT IN (SELECT `name` FROM `score2` WHERE `score1`>=60 AND`score2`>=60 GROUP BY `name`) GROUP BY `name`) sc1 JOIN score2 sc2 ON sc1.`name`=sc2.`name` AND (sc1.max_sc=sc2.score1+sc2.score2) );
3.表三及相關題目(兩表聯合查詢)
order_info表
sh_order表
3d
查詢訂單申請時間在2020年4月份和5月份各申請人的合計訂單總額,結果以訂單總額降序排序
SELECT s.`or_customer` '申請人', SUM(`num`*`price`) '總價' FROM `order_info` o JOIN `sh_order` s ON o.`id`=s.`id` WHERE YEAR(`or_date`)='2020' AND MONTH(`or_date`) IN (4,5) GROUP BY `or_customer` ORDER BY SUM(`num`*`price`) DESC;
去除英國、法國的訂單,查詢各年月的訂單申請總額
SELECT DATE_FORMAT(`or_date`,'%Y-%m'), SUM(`num`*`price`) '總價' FROM `order_info` o JOIN `sh_order` s ON o.`id`=s.`id` WHERE `country` NOT IN('英國','法國') GROUP BY DATE_FORMAT(`or_date`,'%Y-%m') ORDER BY SUM(`num`*`price`) DESC;
4.表四及相關題目(兩表聯合查詢)
pro_info表
sale_info表
code
查詢2020年,每個月的銷售額,以及同比環比,並按照年月進行升序排序
//建立以月份爲維度的銷售額視圖 CREATE VIEW sale_month AS (SELECT YEAR(s.`sale_time`) s_year,MONTH(s.`sale_time`) s_month,SUM(p.`price`*s.`num`) s_total FROM `pro_info` p JOIN `sale_info` s ON p.`pid`=s.`pid` GROUP BY DATE_FORMAT(s.`sale_time`,'%Y-%m') ORDER BY DATE_FORMAT(s.`sale_time`,'%Y-%m')); SELECT CONCAT(s1.s_year,'-',s1.s_month) '時間', s1.s_total '銷售額',((s1.s_total-s2.s_total)/s1.s_total) '環比', ((s1.s_total-s3.s_total)/s1.s_total)'同比' FROM sale_month s1 LEFT JOIN sale_month s2 ON (s1.s_year=s2.s_year AND s1.s_month=s2.s_month+1) OR(s1.s_year=s2.s_year+1 AND s1.s_month=s2.s_month-11) LEFT JOIN sale_month s3 ON s1.s_year=s3.s_year+1 AND s1.s_month=s3.s_month;
查詢2020年起,每個月的累計銷售額
//經過sql引用變量實現銷售額累計 SELECT CONCAT(s_year,'-',s_month) '時間',(@sale_sum:=@sale_sum+s_total) '累計銷售額' FROM sale_month ,(SELECT @sale_sum:=0) sale_sum WHERE s_year=2020;