使用实验楼练习MySQL查询时碰到了这样一个题:
计算每个月中某用户访问网页的天数:
CREATE TABLE t1 (year YEAR(4), month INT(2) UNSIGNED ZEROFILL,
day INT(2) UNSIGNED ZEROFILL);
INSERT INTO t1 VALUES(2000,1,1),(2000,1,20),(2000,1,30),(2000,2,2),
(2000,2,23),(2000,2,23);
select * from t1;
±-----±------±-----+
| year | month | day |
±-----±------±-----+
| 2000 | 01 | 01 |
| 2000 | 01 | 20 |
| 2000 | 01 | 30 |
| 2000 | 02 | 02 |
| 2000 | 02 | 23 |
| 2000 | 02 | 23 |
±-----±------±-----+
上面的表格反馈的是用户访问的年月日,那么很明显一月访问天数为3,二月访问天数为2,当然其中2月23日访问了2次。我开始以为只要查询月份记录就好了,于是我列了这样的代码:
select year,month,count(*) form t1 group by year,month;
返回的结果就变成了:
select year,month,count() from t1 group by year,month;
±-----±------±---------+
| year | month | count() |
±-----±------±---------+
| 2000 | 01 | 3 |
| 2000 | 02 | 3 |
±-----±------±---------+
2 rows in set (0.