练习

1.

 

 select name,family,address
from lianjie
where (name,family) in
(select  name,family
from lianjie
group by name,family
having count(distinct address) > 1
)

 

 或者

SELECT DISTINCT a.name,a.address 
from lianjie a,lianjie b
where b.family=a.family 
and b.name=a.name 
and b.address != a.address

2.

 

 select name,
max(case when course='语文' then 1 else 0 end) as '语文',
max(case when course='数学' then 1 else 0 end) as '数学',
max(case when course='英语' then 1 else 0 end) as '英语'
from lianjie
group by name

 

 3.

 

 select left(班级名,3) as 年级,
sum(人数) as 人数
from lianjie
group by 年级

 

 4.

 

 SELECT 县名,
sum(case 性别 when '男' then 人口 ELSE 0 end) as 男,
sum(case 性别 when '女' then 人口 ELSE 0 end) as 女
from lianjie
group by `县名`

 

 

posted @ 2022-08-31 16:48  萧六弟  阅读(21)  评论(0)    收藏  举报