`

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)

分享到:
评论

相关推荐

    cpu的简单设计

    reg [20:0] count = 21'b0; Instruction_Mem instruction(clock,reset,i_addr,i_datain); PCPU pcpu(clock, enable, reset, start, d_datain, i_datain, select_y, i_addr, d_addr, d_dataout, d_we, y); ...

    sd-reader_source_25218_TheCount_ATmega32128P_

    Implement renaming a file or directory.Rewrite byteorder handling to fix unaligned memory accesses on 32-bit and probably 16-bit architectures.Make fat_create_file() not return failure if the file ...

    wb_lpc_latest.tar.gz_And Yet ..._LPC BIT I/O_VHDL DMA_WISHBONE L

    Wishbone to LPC (Low-Pin Count) Bridge, includes master and slave modules. Supports 8-bit I/O Read and Write cycles, 8-bit Memory Read/Write cycles, DMA cycles, and up to 32-bit Firmware memory read/...

    BIos原代码《陈文钦》

    HARD RESET OR SHUTDOWN RESET ; ;---------------------------------------; ; SHUTDOWN PROCESSING ; ;---------------------------------------; bios_suru: cli ; test under CLI mode cld ; ensure ...

    A.Collection.of.Bit.Programming.Interview.Questions.solved.in.C++

    Compute whether or not an unsigned number is a power of two Chapter 4. Set the i-th bit Chapter 5. Unset the i-th bit Chapter 6. Toggle the i-th bit Chapter 7. Given an unsigned number with only one ...

    Introduction to 64Bit Windows Assembly

    One chapter covers how to efficiently count the 1 bits in an array with the most efficient version using the recently-introduced popcnt instruction. Another chapter covers using SSE instructions to ...

    python实现位操作 Bit Manipulation 课程设计

    python实现: 二进制和运算符 二进制计数设置位 二进制计数尾随零 二进制或运算符 二进制移位 二进制二进制补码 二进制异或运算符 计数 1S 布赖恩·克尼汉方法 计算 1 位数 ... Highest Set Bit

    单片机程序设计 电子钟程序

    CLOSE_BIT EQU 40H ;显示屏蔽(和位选相与后送P2) A1_MINUTE EQU 41H ;闹铃1 分钟 A1_HOUR EQU 42H ;闹铃1 小时 A1_SWITCH EQU 43H ;闹铃1 开关 A2_MINUTE EQU 44H ;闹铃2 分钟 A2_HOUR EQU 45H ;闹铃2...

    一个win32下的ARM开源编译器

    (32-bit) or narrow (16-bit) encodings. If the instruction cannot be encoded in the desired width fasmarm will give an error. If the instruction does not have both wide an narrow forms then the .W and ...

    zynq_canutils.zip

    receive lists, their filters and the count of filter matches can be checked in the appropriate receive list. All entries contain the device and a protocol module identifier: $ cat /proc/...

    This_one.rar_ cooperative ber_cooperative_cooperative control_re

    Cooperative communication_power control ...(e) Perform hard-decision-decoding and count the bit errors (f) Plot the power given to both source(Ps) and relay(Pr) with both SNR and BER

    Verilog 计数器实现

    Design a 4‐bit Up/Down counter that can count up, count down, or remain at the present value. The counter has four inputs RST, CLK, COUNT, and UPDN and one output VALUE. The ENABLE input is ...

    FRP-Linux服务端安装-一键安装脚本

    Please input frps max_pool_count [1-200](Default max_pool_count: 50): #设置每个代理可以创建的连接池上限,默认50 ##### Please select log_level ##### 1: info 2: warn 3: error 4: debug #################...

    linux全志R16的linux系统编译的资料_20170502_1655.7z

    全志R16平台编译linux系统V1.0.txt 2017/4/11 13:36 (编译请使用编译android的lichee的选项编译生成的.config文件,不然直接编译会报错!!...rootroot@cm-System-Product-Name:/home/wwt/linux_r16$ tar zxvf ...

    基于CYCLONE2 FPGA设计的频率计+串口通信实验quartus9.0工程源码+文档说明资料.rar

    基于CYCLONE2 FPGA设计的频率计+串口通信实验quartus9.0工程源码+文档说明资料, /***********************************************************************...always@(posedge clkbaud8x or negedge rst)//clkbaud8x

    数字电路CMOS&TTL74系列芯片ALTIUM库(AD原理图库).SCHLIB

    Library Component Count : 896 4020 14-Stage Ripple-Carry Binary Counter 4021 8-Stage Shift Register (Asynchronous Parallel Input or Synchronous Serial Input/Serial Output) 4022 Octal Counter with 8 ...

    kgb档案压缩console版+源码

    where y_i is the i'th bit, and the context is the previous i - 1 bits of uncompressed data. 2. PAQ6 MODEL The PAQ6 model consists of a weighted mix of independent submodels which make predictions ...

    MediaInfo_GUI_19.09_Windows.exe

    Chapters: count of chapters, list of chapters... MediaInfo analyticals include: Container: MPEG-4, QuickTime, Matroska, AVI, MPEG-PS (including unprotected DVD), MPEG-TS (including unprotected Blu-...

    sqlserver自定义函数

    @flag bit --转换标志,0转换成半角,1转换成全角 )RETURNS nvarchar(4000) AS BEGIN DECLARE @pat nvarchar(8),@step int,@i int,@spc int IF @flag=0 Select @pat=N'%[!-~]%',@step=-65248, @str=...

    MD5加密算法(Java语言描述)

    MD5加密算法(Java版) 可以运行 原理  对MD5算法简要的叙述可以为:MD5以512位分组来处理输入的信息,且每一分组又被划分为16个32位子分组,经过了一系列的处理后,算法的输出由四个32位分组组成,将这四个32位...

Global site tag (gtag.js) - Google Analytics