mysql位函数的使用

查询每个月的访问天数

mysql> create table t1 (year YEAR(4),month int(2) unsigned zerofill,day int(2) u
nsigned zerofill);
mysql> select * from t1;
+------+-------+------+
| year | month | day  |
+------+-------+------+
| 2000 |    01 |   01 |
| 2000 |    01 |   20 |
| 2000 |    01 |   30 |
| 2000 |    02 |   02 |
| 2000 |    02 |   23 |
| 2000 |    02 |   23 |
+------+-------+------+

因为同一天会访问多次,所以仅仅使用group by year,month不准确,可以采用bit_count和bit_or来统计

SELECT year,month,BIT_COUNT(BIT_OR(1<<day)) AS days FROM t1 group by year,month

 

posted @ 2019-03-06 10:11  远去的列车  阅读(406)  评论(0编辑  收藏  举报