当前位置:网站首页>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 operationMediumOr operationsOf .BIT_COUNT: This function is used to calculate 1 The number of .1 << day: Express 1 Moving to the left day bits .1 << 1The value is 0000 0000 0000 0000 0000 0000 0000 00101 << 20The value is 0000 0000 0001 0000 0000 0000 0000 00001 << 30The value is 0000 0001 0000 0000 0000 0000 0000 0000
For this 3 Value for
Or operationsWords , 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.

边栏推荐
- 容斥原理 AcWing 890. 能被整除的数
- Is it impossible for lamda to wake up?
- Leetcode-556: the next larger element III
- liunx启动redis
- Appium foundation - use the first demo of appium
- [learning] database: several cases of index failure
- Règlement sur la sécurité des réseaux dans les écoles professionnelles secondaires du concours de compétences des écoles professionnelles de la province de Guizhou en 2022
- Daily question 1189 Maximum number of "balloons"
- MySQL怎么运行的系列(八)14张图说明白MySQL事务原子性和undo日志原理
- Leetcode array operation
猜你喜欢

Sqlmap tutorial (1)

Sorting out the latest Android interview points in 2022 to help you easily win the offer - attached is the summary of Android intermediate and advanced interview questions in 2022

Operator priority, one catch, no doubt

redis发布订阅命令行实现
![[2020]GRAF: Generative Radiance Fields for 3D-Aware Image Synthesis](/img/20/826cc9d514496955a557439881234d.jpg)
[2020]GRAF: Generative Radiance Fields for 3D-Aware Image Synthesis

QQ computer version cancels escape character input expression

阿里新成员「瓴羊」正式亮相,由阿里副总裁朋新宇带队,集结多个核心部门技术团队

2021apmcm post game Summary - edge detection

MPLS experiment

1.13 - RISC/CISC
随机推荐
How to understand the definition of sequence limit?
Arduino 控制的 RGB LED 无限镜
LeetCode-54
MySQL advanced part 1: stored procedures and functions
Leetcode-3: Longest substring without repeated characters
How to make water ripple effect? This wave of water ripple effect pulls full of retro feeling
MySQL advanced part 2: the use of indexes
MySQL advanced part 2: optimizing SQL steps
博弈论 AcWing 894. 拆分-Nim游戏
Is it impossible for lamda to wake up?
Leetcode recursion
求组合数 AcWing 887. 求组合数 III
liunx启动redis
MySQL advanced part 2: SQL optimization
New title of module a of "PanYun Cup" secondary vocational network security skills competition
A reason that is easy to be ignored when the printer is offline
Appium automation test foundation - Summary of appium test environment construction
打印机脱机时一种容易被忽略的原因
【LeetCode】Day95-有效的数独&矩阵置零
How to set the drop-down arrow in the spinner- How to set dropdown arrow in spinner?