sql - 连接sqlite中未知数量的值

标签 sql sqlite concatenation

我正在使用 sqlite 3.15.1 。

我有一个表master,其中包含大学的主时间表。

看起来像:

day         sem         sec         hour        sub_id    
----------  ----------  ----------  ----------  ----------   
MON         5           B           4           10IS51 
MON         5           B           4           10IS53   
MON         5           B           5           10CS54    
MON         5           B           6           10CS55    
MON         5           B           7           10CS53    
MON         3           A           1           10CS33   

还有更多的值(value)......

相同的其他值有多个 sub_id ,这意味着 - 在周一第一个小时,5th B 学生可能有 2 个或更多实验室 (sub_id)。 (分批进行)。

为了获得正确的时间表,我正在这样做:

select day,
max( case when hour =1 then sub_id end ) as 'hour-1',
max( case when hour =2 then sub_id end ) as 'hour-2',
max( case when hour =3 then sub_id end ) as 'hour-3',
max( case when hour =4 then sub_id end ) as 'hour-4',
max( case when hour =5 then sub_id end ) as 'hour-5',
max( case when hour =6 then sub_id end ) as 'hour-6',
max( case when hour =7 then sub_id end ) as 'hour-7',
max( case when hour =8 then sub_id end ) as 'hour-8'
from master
where sem=5 and sec='B'
group by day
order by day;

但是当出现多个值时,它只给出一个值,即 max() 值。当我使用 min() 时,我得到 min() 值。 我怎样才能同时获得两者?

结果 View 如下所示:

day         hour-1      hour-2      hour-3      hour-4      hour-5      hour-6      hour-7      hour-8    
----------  ----------  ----------  ----------  ----------  ----------  ----------  ----------  ----------
FRI         10CS52      10CS54      10CS53      10CS55      HRD         HRD         TUT                   
MON         10CSL58     10CSL58     10CSL58     10IS51      10CS54      10CS55      10CS53                
SAT         10IS51      10CS55      10CS56      10CS52                                                    
THU         10CS53      10IS51      10CS54      10CS52                                                    
TUE         10CS54      10CS52      10CS56      10CS56                                                    
WED         10CS56      10IS51      10CS53      10CS55      CSA         CSA         CSA                   

但我想要这样的东西:

day         hour-1           hour-2          hour-3      hour-4      hour-5      hour-6      hour-7      hour-8    
----------  ----------      ----------       ----------  ----------  ----------  ----------  ----------  ----------
FRI         10CS52,10CS53   10CS54           10CS53      10CS55      HRD         HRD         TUT                   
MON         10CSL58         10CSL58,10CSL33  10CSL58     10IS51      10CS54      10CS55      10CS53                
SAT         10IS51,10IS48   10CS55           10CS56      10CS52                                                    
THU         10CS53          10IS51           10CS54      10CS52                                                    
TUE         10CS54          10CS52           10CS56      10CS56                                                    
WED         10CS56          10IS51           10CS53      10CS55      CSA         CSA         CSA   

也就是说,所有类 - 逗号分隔,而不是 min() 或 max()。

有可能实现这个目标吗?请帮助我。

谢谢。

最佳答案

将 MIN/MAX 替换为 GROUP_CONCAT

select day,
group_concat( case when hour =1 then sub_id end ) as 'hour-1',
group_concat( case when hour =2 then sub_id end ) as 'hour-2',
group_concat( case when hour =3 then sub_id end ) as 'hour-3',
group_concat( case when hour =4 then sub_id end ) as 'hour-4',
group_concat( case when hour =5 then sub_id end ) as 'hour-5',
group_concat( case when hour =6 then sub_id end ) as 'hour-6',
group_concat( case when hour =7 then sub_id end ) as 'hour-7',
group_concat( case when hour =8 then sub_id end ) as 'hour-8'
from master
where sem=5 and sec='B'
group by day
order by day;

关于sql - 连接sqlite中未知数量的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40688895/

相关文章:

php - 在 laravel 中插入关系

Android SQLiteDatabase查询排序顺序

iOS CoreData - 启用 sqlite WAL/Write-Ahead Logging 有什么缺点吗

jquery - 使用 jquery 将 XML 解析为 html5 数据库

将用户输入连接到 C 中的字符串

ffmpeg - 连接两个 .mov 文件会产生相同大小的文件

php - 列连接返回 "Null"Mysql - Php

sql - 艰难地学习 SQL - 练习 3 - 插入数据 - 找不到文件

sql - 如何转换选择查询

mysql - 如何使用 where 子句进行 UPSERT