zoukankan      html  css  js  c++  java
  • BIT_COUNT()和BIT_OR()

    在学习MySQL手册时,看到根据天数计算访问量时,出现了BIT_COUNT()和BIT_OR()两个函数来处理天数计算的问题

    所使用的表格信息如下:

    mysql> select year,month,day from t1;

    +------+-------+------+
    | year | month | day  |
    +------+-------+------+
    | 2000 |    01 |   01 |
    | 2000 |    01 |   20 |
    | 2000 |    01 |   30 |
    | 2000 |    02 |   02 |
    | 2000 |    02 |   23 |
    | 2000 |    03 |   13 |
    | 2000 |    02 |   23 |
    +------+-------+------+
    7 rows in set (0.00 sec)

    在处理这个问题时,起初我也没有多想,无非是根据每个月的天数输出访问量,于是我做了如下的查询:

    mysql> select year,month,count(day) as day from t1 group by    

        -> year,month;

    +------+-------+-----+
    | year | month | day |
    +------+-------+-----+
    | 2000 |    01 |   3 |
    | 2000 |    02 |   3 |
    | 2000 |    03 |   1 |
    +------+-------+-----+
    3 rows in set (0.02 sec)

    但是,此时我发现2月份有两个2月23日,这样没有去掉重复的天数,导致结果错误,看了手册的查询过程如下:

    mysql> 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 |
    | 2000 |    03 |    1 |
    +------+-------+------+
    3 rows in set (0.02 sec)

    它使用了BIT_COUNT()和BIT_OR()两个函数,这两个函数的用法如下:

    1、BIT_COUNT( expr ):返回 expr 的二进制表达式中”1“的个数。

         例如:29 = 11101 则:BIT_COUNT(29)= 4;

    2、BIT_OR( expr ):返回 expr 中所有比特的bitwise OR。计算执行的精确度为64比特(BIGINT) 。    例如:上面例子中,2000年02月中有一条2号的记录两条23号的记录,所以"1<<day"表示出来就是 “1<<2”和“1<<23”,得到二进制数 100 和 100000000000000000000000 。然后再OR运算。即 100 OR 10000000000000000000000 OR 10000000000000000000000 = 100000000000000000000100;这样再用BIT_COUNT处理得出的值就是2,自动去除了重复的日期。

    但是,我觉得这种使用2进制来进行计算的方法有些麻烦,我采用了一下的办法来解决这个问题:

    mysql> select year,month,count(distinct day) as day from t1 group by    

    -> year,month;

    +------+-------+-----+
    | year | month | day |
    +------+-------+-----+
    | 2000 |    01 |   3 |
    | 2000 |    02 |   2 |
    | 2000 |    03 |   1 |
    +------+-------+-----+
    3 rows in set (0.02 sec)

    其实这种方法也能解决这个问题,至少在我看来不必跳的那么远去解决这个问题。

  • 相关阅读:
    信息和熵
    【算法】欧几里得算法与青蛙约会oj
    【算法】并查集模板与练习
    Kaggle-房价预测
    【pytorch】pytorch-backward()的理解
    【pytorch】pytorch-LSTM
    【计算机网络】数据链路层总结
    【Code】numpy、pytorch实现全连接神经网络
    【MT】牛津的MT教程
    【计算机网络】物理层基础知识
  • 原文地址:https://www.cnblogs.com/yabingshi/p/3870398.html
Copyright © 2011-2022 走看看