Sql中常见的刁钻写法(心得记录)

1.Sql中常见的刁钻写法(心得记录)_第1张图片

 

HAVING count(cid)=(SELECT count(cid) FROM SC WHERE sid='01')

2.

Sql中常见的刁钻写法(心得记录)_第2张图片

SELECT a.*,b.Cname FROM
(SELECT CId,
MAX(score) as 最高分,
MIN(score) as 最低分,
AVG(score) as 平均分,
COUNT(1)  as 选修人数,
SUM(case when score>=60 then 1 else 0 end) / COUNT(1) as 及格率,
SUM(case when score>=70 and score< 80 then 1 else 0 end) / COUNT(1) as 中等率,
SUM(case when score>=80 and score< 90 then 1 else 0 end) / COUNT(1) as 优良率,
SUM(case when score>=90 then 1 else 0 end) / COUNT(1) as 优秀率
FROM SC
GROUP BY CId) a left join Course b on a.CId=b.CId
ORDER BY 选修人数 DESC,CId ASC

Sql中常见的刁钻写法(心得记录)_第3张图片 

select *,
sum(case when 0<=score and score <=60 then 1 else 0 end )/count(1) as '[0,60]',
sum(case when 60 sum(case when 70 sum(case when 85 from SC 
group by cid

3.Sql中常见的刁钻写法(心得记录)_第4张图片 

4.计算年龄

Sql中常见的刁钻写法(心得记录)_第5张图片 

select *, year(NOW())-year(sage) as 年龄
from Student

5.

Sql中常见的刁钻写法(心得记录)_第6张图片

6.

Sql中常见的刁钻写法(心得记录)_第7张图片 

7.

Sql中常见的刁钻写法(心得记录)_第8张图片 

 

 

你可能感兴趣的:(sql,mybatis,数据库)