当前位置:网站首页>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.
边栏推荐
- Golang uses context gracefully
- In depth analysis of for (VaR I = 0; I < 5; i++) {settimeout (() => console.log (I), 1000)}
- WordPress switches the page, and the domain name changes back to the IP address
- Sqlmap tutorial (1)
- Usage scenarios of golang context
- 高斯消元 AcWing 884. 高斯消元解异或线性方程组
- SPI 详解
- C job interview - casting and comparing - C job interview - casting and comparing
- RGB LED infinite mirror controlled by Arduino
- [rust notes] 14 set (Part 1)
猜你喜欢
Arduino 控制的 RGB LED 无限镜
做 SQL 性能优化真是让人干瞪眼
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
Appium automation test foundation - Summary of appium test environment construction
开源存储这么香,为何我们还要坚持自研?
1.14 - assembly line
Groupbykey() and reducebykey() and combinebykey() in spark
传统数据库逐渐“难适应”,云原生数据库脱颖而出
Appium foundation - use the first demo of appium
20220213-CTF MISC-a_ good_ Idea (use of stegsolve tool) -2017_ Dating_ in_ Singapore
随机推荐
What's wrong with this paragraph that doesn't work? (unresolved)
打印机脱机时一种容易被忽略的原因
LeetCode-54
Introduction to LVS [unfinished (semi-finished products)]
Leetcode dynamic programming
SPI 详解
C Primer Plus Chapter 15 (bit operation)
1.13 - RISC/CISC
Erreur de connexion Navicat à la base de données Oracle Ora - 28547 ou Ora - 03135
【LeetCode】Day95-有效的数独&矩阵置零
求组合数 AcWing 889. 满足条件的01序列
Gauss Cancellation acwing 884. Solution d'un système d'équations Xor linéaires par élimination gaussienne
Daily question 1189 Maximum number of "balloons"
Matrixdb V4.5.0 was launched with a new mars2 storage engine!
Simple selection sort of selection sort
Series of how MySQL works (VIII) 14 figures explain the atomicity of MySQL transactions and the principle of undo logging
Sword finger offer II 058: schedule
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
A reason that is easy to be ignored when the printer is offline
Groupbykey() and reducebykey() and combinebykey() in spark