

# 想要没有学生的部门也保存,就得以部门表作为主表
#字母表顺序就是升序
SELECT d.dept_name,IFNULL(s1.number,0) student_number
FROM Department d
LEFT JOIN
(SELECT dept_id,count(*) numberFROM Student sGROUP BY dept_id
)s1
ON d.dept_id=s1.dept_id
ORDER BY s1.number desc,d.dept_name asc;




SELECT c.country_name,w.weather_type
FROM
Countries c
INNER JOIN
(SELECT country_id,if(avg(weather_state)<=15,'Cold',(if(avg(weather_state)>=25,'Hot','Warm'))) weather_typeFROM Weather WHERE date_format(day,"%Y-%m")='2019-11'GROUP BY country_id
)w
ON w.country_id=c.country_id;



# case when then和if有时可以实现一样的功能
# MOD(N, M) 可以得到n除以m后的余数
SELECT IF(MOD(id,2)=0,id-1,IF(id=(SELECT count(*) FROM Seat),id,id+1)) id,student
FROM Seat
ORDER BY id asc;

上一篇:C语言文件操作