我正在编写一个计划来安排学校的时间表。我在Qt中使用QSQLITE
和c ++。
我有一张桌子如下图所示。这些值由用户给出。
+------+------+------------------+
| day | hour | sub |
+------+------+------------------+
| Mon | 1 | Maths |
| Mon | 2 | Physics |
| Mon | 3 | Chemistry |
| Tue | 1 | Biology |
| Tue | 2 | P.H.E |
| Tue | 3 | Computer Science |
| Wed | 1 | Physics |
| Wed | 2 | Chemistry Lab |
| Wed | 3 | Chemistry Lab |
+------+------+------------------+
我想将视图更改为:
+------+----------+-----------------+---------------------+
| day | 1 | 2 | 3 |
+------+----------+-----------------+---------------------+
| Mon | Maths | Physics | Chemistry |
| Tue | Biology | P.H.E | Computer Science |
| Wed | Physics | Chemistry Lab | Chemistry Lab |
+------+------+---------------------+---------------------+
有可能吗?
注意:如果仅使用SQL命令是不可能的,那么我可以使用C ++。
感谢。
答案 0 :(得分:2)
您可以使用条件聚合:
select day,
max(case when hour = 1 then sub end) as hour_1,
max(case when hour = 2 then sub end) as hour_2,
max(case when hour = 3 then sub end) as hour_3
from t
group by day;