当前位置:网站首页>Bit of MySQL_ OR、BIT_ Count function
Bit of MySQL_ OR、BIT_ Count function
2022-07-05 06:21:00 【StoNENeee】
1. Preface
Recently in to see MySQL Official documents , Find a very clever usage , You can quickly calculate the number of days a user visits the system every month . Now let's analyze the principle .
2. Data preparation
CREATE TABLE data_test (year YEAR, month INT UNSIGNED, day INT UNSIGNED);
INSERT INTO data_test VALUES(2022,1,1),(2022,1,20),(2022,1,30),(2022,2,2),(2022,2,5),(2022,2,5);
From the above table creation statement and data insertion statement 2022 Year of 1 The number of visit days in the month is 3,2 The number of visit days in the month is 2.
According to the general query , You can query according to the following statement :
with a as (select `year`, `month` from data_test group by `year`, `month`)
select b.`year`, b.`month`, count(distinct day) days
from a
left join data_test b on a.`month` = b.`month` and a.`year` = b.`year`
group by b.`year`, b.`month`;
+------+-------+------+
| year | month | days |
+------+-------+------+
| 2022 | 1 | 3 |
| 2022 | 2 | 2 |
+------+-------+------+
3. Data query
As usual , We need to check twice to get the result . In fact, we have a simpler way to query , Is the use of MySQL For us BIT_OR
、BIT_COUNT
function .
SELECT `year`, `month`, BIT_COUNT(BIT_OR(1 << day)) AS days
FROM data_test
GROUP BY `year`, `month`;
+------+-------+------+
| year | month | days |
+------+-------+------+
| 2022 | 1 | 3 |
| 2022 | 2 | 2 |
+------+-------+------+
You can see that the results obtained are consistent with the results of the above query using the conventional method , So what is the principle of these two functions ? Let's analyze one by one .
BIT_OR
: This function is used toAn operation
MediumOr operations
Of .BIT_COUNT
: This function is used to calculate 1 The number of .1 << day
: Express 1 Moving to the left day bits .1 << 1
The value is 0000 0000 0000 0000 0000 0000 0000 00101 << 20
The value is 0000 0000 0001 0000 0000 0000 0000 00001 << 30
The value is 0000 0001 0000 0000 0000 0000 0000 0000
For this 3 Value for
Or operations
Words , You can get 0000 0001 0001 0000 0000 0000 0000 0010, take 1 The number of 3
therefore MySQL Get the result through very clever bit operation , It also improves performance .
Last , Welcome to WeChat official account.
边栏推荐
- MySQL advanced part 2: storage engine
- Chart. JS - Format Y axis - chart js - Formatting Y axis
- Records of some tools 2022
- Matrixdb V4.5.0 was launched with a new mars2 storage engine!
- SPI details
- 博弈论 AcWing 892. 台阶-Nim游戏
- Niu Mei's math problems
- MPLS experiment
- LeetCode 0108. Convert an ordered array into a binary search tree - the median of the array is the root, and the left and right of the median are the left and right subtrees respectively
- 博弈论 AcWing 891. Nim游戏
猜你喜欢
SQL三种连接:内连接、外连接、交叉连接
Groupbykey() and reducebykey() and combinebykey() in spark
Simple selection sort of selection sort
QQ computer version cancels escape character input expression
可变电阻器概述——结构、工作和不同应用
2021apmcm post game Summary - edge detection
求组合数 AcWing 889. 满足条件的01序列
How to make water ripple effect? This wave of water ripple effect pulls full of retro feeling
1.15 - input and output system
Alibaba's new member "Lingyang" officially appeared, led by Peng Xinyu, Alibaba's vice president, and assembled a number of core department technical teams
随机推荐
Sqlmap tutorial (II) practical skills I
Appium automation test foundation - Summary of appium test environment construction
927. Trisection simulation
TypeScript 基础讲解
[rust notes] 16 input and output (Part 2)
Daily question 1189 Maximum number of "balloons"
Leetcode-6109: number of people who know secrets
20220213-CTF MISC-a_ good_ Idea (use of stegsolve tool) -2017_ Dating_ in_ Singapore
【LeetCode】Day94-重塑矩阵
MySQL advanced part 1: View
[2021]IBRNet: Learning Multi-View Image-Based Rendering Qianqian
One question per day 1020 Number of enclaves
Applicable to Net free barcode API [off] - free barcode API for NET [closed]
1.14 - assembly line
11-gorm-v2-02-create data
LeetCode 1200. Minimum absolute difference
Bash exercise 17 writing scripts to install the server side of FRP reverse proxy software
[rust notes] 13 iterator (Part 2)
MySQL advanced part 2: the use of indexes
Navicat連接Oracle數據庫報錯ORA-28547或ORA-03135