- 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,3),(2000,2,3);
- 示例表中含有代表用户访问网页的年-月-日值。可以使用以下查询来确定每个月的访问天数:
- SELECT year,month,BIT_COUNT(BIT_OR(1<<day)) AS days FROM t1
- GROUP BY year,month;
- 将返回:
- +------+-------+------+
- | year | month | days |
- +------+-------+------+
- | 2000 | 01 | 3 |
- | 2000 | 02 | 2 |
- +------+-------+------+
- 该查询计算了在表中按年/月组合的不同天数,可以自动去除重复的询问。
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,3),(2000,2,3); 示例表中含有代表用户访问网页的年-月-日值。可以使用以下查询来确定每个月的访问天数: SELECT year,month,BIT_COUNT(BIT_OR(1<<day)) AS days FROM t1 GROUP BY year,month; 将返回: +------+-------+------+ | year | month | days | +------+-------+------+ | 2000 | 01 | 3 | | 2000 | 02 | 2 | +------+-------+------+ 该查询计算了在表中按年/月组合的不同天数,可以自动去除重复的询问。
<<位运算 使1左移了day位。
1<<2=0100 1<<3=1000
bit_or:相同值or运算后值一样,最后把不一样的值加起来。
1000 or 1000 or 0100 = 1100
bit_count:统计被制1的个数。
bit_count(1100) = 2;
说白了意思就是统计被制1的个数。
'IT' 카테고리의 다른 글
Workarounds Issued For 'Apache Killer' Attack (0) | 2011.08.27 |
---|---|
AUTO_INCREMENT (0) | 2011.08.26 |
Using User-Defined Variables (0) | 2011.08.26 |
Getting Information About Databases and Tables (0) | 2011.08.26 |
using More Than one Table (0) | 2011.08.26 |