一道SQL面试题

2019-01-02 21:31发布

标签:
4条回答
皆成旧梦
2楼-- · 2019-01-02 21:49

面试要手写这语句要骂娘。

查看更多
不流泪的眼
3楼-- · 2019-01-02 21:59

MYSQL的
select
a.时间,ifnull(group_concat(a.周一), '无课') 周一,
ifnull(group_concat(a.周二), '无课') 周二,
ifnull(group_concat(a.周三), '无课') 周三,
ifnull(group_concat(a.周四), '无课') 周四,
ifnull(group_concat(a.周五), '无课') 周五
from
(
SELECT
'上午' as 时间,
group_concat( case week_dat when '周一' then class else null end) AS 周一,
group_concat( case week_dat when '周二' then class else null end) AS 周二,
group_concat( case week_dat when '周三' then class else null end) AS 周三,
group_concat( case week_dat when '周四' then class else null end) AS 周四,
group_concat( case week_dat when '周五' then class else null end) AS 周五
from class_detail
where morning is not null
GROUP BY week_dat
union all

SELECT
'下午' as 时间,
group_concat( case week_dat when '周一' then class else null end) AS 周一,
group_concat( case week_dat when '周二' then class else null end) AS 周二,
group_concat( case week_dat when '周三' then class else null end) AS 周三,
group_concat( case week_dat when '周四' then class else null end) AS 周四,
group_concat( case week_dat when '周五' then class else null end) AS 周五
from class_detail
where afternoon is not null
GROUP BY week_dat
) a
group by 时间

查看更多
怪性笑人.
4楼-- · 2019-01-02 22:03

https://www.cnblogs.com/WangShuaishuai/p/9407917.html

查看更多
荒废的爱情
5楼-- · 2019-01-02 22:07

select 日期, IFNULL(Max(case m.周期 when '周一' then 科目 end),"无课") as '周一' ,
IFNULL(Max(case m.周期 when '周二' then 科目 end),"无课") as '周二' ,
IFNULL(Max(case m.周期 when '周三' then 科目 end),"无课") as '周三',
IFNULL(Max(case m.周期 when '周四' then 科目 end) ,"无课")as '周四',
IFNULL(Max(case m.周期 when '周五' then 科目 end),"无课") as '周五' from (
select 周期,GROUP_CONCAT(科目) as 科目,'上午' as 日期 from 课表 where 上午='有课' GROUP BY 周期 ) m GROUP BY 日期
UNION
select 日期, IFNULL( Max(case m.周期 when '周一' then 科目 end),"无课")as '周一' ,
IFNULL(Max(case m.周期 when '周二' then 科目 end),"无课") as '周二' ,
IFNULL(Max(case m.周期 when '周三' then 科目 end),"无课") as '周三',
IFNULL(Max(case m.周期 when '周四' then 科目 end),"无课") as '周四',
IFNULL(Max(case m.周期 when '周五' then 科目 end),"无课") as '周五' from (
select 周期,GROUP_CONCAT(科目) as 科目,'下午' as 日期 from 课表 where 下午='有课' GROUP BY 周期 ) m GROUP BY 日期                                                                                                            如果有更简单的写法,请大佬指正~~~

查看更多
登录 后发表回答