当前位置:网站首页>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.

边栏推荐
- Record the process of configuring nccl and horovod in these two days (original)
- Nested method, calculation attribute is not applicable, use methods
- 什么是套接字?Socket基本介绍
- Regulations for network security events of vocational group in 2022 Guizhou Vocational College skill competition
- 求组合数 AcWing 888. 求组合数 IV
- C - XOR to all (binary topic)
- Leetcode stack related
- Leetcode-3: Longest substring without repeated characters
- QQ computer version cancels escape character input expression
- 高斯消元 AcWing 884. 高斯消元解异或線性方程組
猜你喜欢

博弈论 AcWing 891. Nim游戏

P2575 master fight

区间问题 AcWing 906. 区间分组

开源存储这么香,为何我们还要坚持自研?

Doing SQL performance optimization is really eye-catching
![[2020]GRAF: Generative Radiance Fields for 3D-Aware Image Synthesis](/img/20/826cc9d514496955a557439881234d.jpg)
[2020]GRAF: Generative Radiance Fields for 3D-Aware Image Synthesis

【LeetCode】Easy | 20. Valid parentheses
![[2021]IBRNet: Learning Multi-View Image-Based Rendering Qianqian](/img/f1/e7a8a1a31bc5712d9f32d91305a2b0.jpg)
[2021]IBRNet: Learning Multi-View Image-Based Rendering Qianqian

Sqlmap tutorial (II) practical skills I

MySQL advanced part 2: optimizing SQL steps
随机推荐
Operator priority, one catch, no doubt
Sqlmap tutorial (1)
Winter vacation water test 1 Summary
Navicat連接Oracle數據庫報錯ORA-28547或ORA-03135
Traversal of leetcode tree
How to understand the definition of sequence limit?
MySQL advanced part 1: stored procedures and functions
MySQL advanced part 1: View
做 SQL 性能优化真是让人干瞪眼
Data visualization chart summary (II)
Usage scenarios of golang context
Leetcode array operation
Data visualization chart summary (I)
MySQL advanced part 2: MySQL architecture
Navicat连接Oracle数据库报错ORA-28547或ORA-03135
Multi screen computer screenshots will cut off multiple screens, not only the current screen
There are three kinds of SQL connections: internal connection, external connection and cross connection
Golang uses context gracefully
Leetcode recursion
C - XOR to all (binary topic)