一个题目,觉得这是个非常经典的SQL语句。所以拿出来大家分享。因为想不出怎么叫合适,所以就暂且叫做空字段吧。
问题:
t1 t2
id firstTime usid lastTime
1 2007-5-1 1 2007-5-2 00:5:01
2 2007-5-1 2 2007-5-2 05:06:12
3 2007-5-2 3 2007-5-6 12:01:15
4 2007-5-3 4 2007-6-2 15:11:12
5 2007-5-3 5 2007-5-8 00:00:05
6 2007-5-4
7 2007-6-1
8 2007-6-2
9 2007-6-2
输出结果:
time count(firstTime) count(lastTime)
2007-5-1 2 0
2007-5-2 1 2
2007-5-3 2 0
2007-5-4 1 0
2007-5-5 0 0
2007-5-6 0 0
2007-5-7 0 0
2007-5-8 0 0
...........
...........
2007-6-2 2
我给的答案:
问题:
t1 t2
id firstTime usid lastTime
1 2007-5-1 1 2007-5-2 00:5:01
2 2007-5-1 2 2007-5-2 05:06:12
3 2007-5-2 3 2007-5-6 12:01:15
4 2007-5-3 4 2007-6-2 15:11:12
5 2007-5-3 5 2007-5-8 00:00:05
6 2007-5-4
7 2007-6-1
8 2007-6-2
9 2007-6-2
输出结果:
time count(firstTime) count(lastTime)
2007-5-1 2 0
2007-5-2 1 2
2007-5-3 2 0
2007-5-4 1 0
2007-5-5 0 0
2007-5-6 0 0
2007-5-7 0 0
2007-5-8 0 0
...........
...........
2007-6-2 2
我给的答案:
CREATE TABLE `t1` ( |
输出:
query result(8 records)
time | f_t | l_t |
2007-05-01 |
2 | 0 |
2007-05-02 |
1 | 0 |
2007-05-03 |
2 | 0 |
2007-05-04 |
1 | 0 |
2007-05-06 |
0 | 1 |
2007-05-08 |
0 | 1 |
2007-06-01 |
1 | 0 |
2007-06-02 |
2 | 0 |
本文转自 david_yeung 51CTO博客,原文链接:http://blog.51cto.com/yueliangdao0608/81308,如需转载请自行联系原作者