当前位置:网站首页>MySQL field userid comma separated save by userid query
MySQL field userid comma separated save by userid query
2022-07-03 04:07:00 【ITKEY_】
my sql Average level , Just a note . Unable to save is the optimal solution . For reference only .
scene
There is a message table , There is a recipient field in which multiple users are listed as , Separate and save information . I need to be based on userid To search for information . For ease of understanding , Let me reduce the table structure . My table structure is as follows :
| message_id | receiver | sender | message | create_time |
|---|---|---|---|---|
| 1 | user1,user2 | system | hello | 2022-06-30 16:30:12 |
| 2 | user1,user3 | system | world | 2022-06-30 16:31:19 |
| 3 | user3,user4,user5 | user1 | java | 2022-06-30 16:31:22 |
| 4 | user7,user8 | user2 | neovim | 2022-06-30 16:31:53 |
| 5 | itkey,lxyoucan | system | itkey | 2022-06-30 16:32:17 |
| 6 | user,user11 | good | very | 2022-06-30 16:38:18 |
Table structure
create table t_message
(
message_id bigint auto_increment comment ' news ID'
primary key,
receiver varchar(4000) null comment ' The recipient ',
sender varchar(200) not null comment ' Sender ',
message varchar(4000) not null comment ' The message content ',
create_time datetime null comment ' Creation time '
)
comment ' Message center ';
data
INSERT INTO t_message (message_id, receiver, sender, message, create_time) VALUES (1, 'user1,user2', 'system', 'hello', '2022-06-30 16:30:12');
INSERT INTO t_message (message_id, receiver, sender, message, create_time) VALUES (2, 'user1,user3', 'system', 'world', '2022-06-30 16:31:19');
INSERT INTO t_message (message_id, receiver, sender, message, create_time) VALUES (3, 'user3,user4,user5', 'user1', 'java', '2022-06-30 16:31:22');
INSERT INTO t_message (message_id, receiver, sender, message, create_time) VALUES (4, 'user7,user8', 'user2', 'neovim', '2022-06-30 16:31:53');
INSERT INTO t_message (message_id, receiver, sender, message, create_time) VALUES (5, 'itkey,lxyoucan', 'system', 'itkey', '2022-06-30 16:32:17');
INSERT INTO t_message (message_id, receiver, sender, message, create_time) VALUES (6, 'user,user11', 'good', 'very', '2022-06-30 16:38:18');
practice
[ False demonstration ]like Fuzzy matching
First of all, my first thought is to use like Statement for fuzzy query . It seems simple and easy to use , But there will be a bug.
Such as query user3:
select * from t_message where receiver like '%user3%';
| message_id | receiver | sender | message | create_time |
|---|---|---|---|---|
| 2 | user1,user3 | system | world | 2022-06-30 16:31:19 |
| 3 | user3,user4,user5 | user1 | java | 2022-06-30 16:31:22 |
No problem , But if I want to inquire user User data ?
select * from t_message where receiver like '%user%';
| message_id | receiver | sender | message | create_time |
|---|---|---|---|---|
| 1 | user1,user2 | system | hello | 2022-06-30 16:30:12 |
| 2 | user1,user3 | system | world | 2022-06-30 16:31:19 |
| 3 | user3,user4,user5 | user1 | java | 2022-06-30 16:31:22 |
| 4 | user7,user8 | user2 | neovim | 2022-06-30 16:31:53 |
| 6 | user,user11 | good | very | 2022-06-30 16:38:18 |
If you find that the data queried at this time is not prepared ,userid The shorter it is , The less accurate it is .
workable proposition
First of all , Probably not the best solution , For reference only . If you have a better way , Welcome to comment area .
help_topic
Before we start, let's have a brief understanding help_topic, It is mainly used to turn one line into multiple lines .
select substring_index(substring_index('82,83,84,85,86', ',', help_topic_id + 1), ',', -1) as Id
from mysql.help_topic
where help_topic_id < (length('82,83,84,85,86') - length(replace('82,83,84,85,86', ',', '')) + 1);
Execution results :
| Id |
|---|
| 82 |
| 83 |
| 84 |
| 85 |
| 86 |
analysis
At first glance, it seems more convoluted , I try to write in detail .
substring_indexThe role of : Get the... On the left side of the target string n The left part of the separator ,n When it is negative, return to the right n The right part of a .help_topicIt's the databasemysqlA table of , This table provides details of the query help topic for a given keyword ( Detailed help information )
Table field meaning :help_topic_id: Help topic details correspond to... In the table record IDname: Help topic given keyword name .help_category_id: Help topic categories ID, And help_category In the table help_category_id Field values are equal .description: Help topic details ( Here's what we really want to see with help information , for example : Tell us how to use the syntax and precautions of XX sentence ).example: Sample information for help topics ( Tell us an example of how statements are used ).url: This help topic corresponds to MySQL In the official online manual URL Link address .
ps: This article sql The sentence is actually the same as help_topic The table has nothing to do with , Just borrowing help_topic Tabular help_topic_id The value of is (0,1,2,…), Therefore, a database containing only ID Fields , It can also achieve the effect of turning to multiple lines
where Query criteria
(length('82,83,84,85,86') - length(replace('82,83,84,85,86', ',', '')) + 1)
It's been a long time , In fact, it is for calculation , Number of separated partial messages . That is to say , The number of +1. In fact, the ultimate goal is just to get one 0,1,2,3,4 A list of . There is such a message , It can be used to intercept strings .
You can put this sql Carry out one paragraph at a time , Until you fully understand .
Final realization
SELECT
message_id,
substring_index( substring_index( msg.receiver, ',', topic.help_topic_id + 1 ), ',',- 1 ) AS receiver,
sender,
message,
create_time
FROM
t_message msg
JOIN mysql.help_topic topic ON topic.help_topic_id < ( length( msg.receiver ) - length( REPLACE ( msg.receiver, ',', '' ) ) + 1 );
The query results are as follows :
| message_id | receiver | sender | message | create_time |
|---|---|---|---|---|
| 1 | user1 | system | hello | 2022-06-30 16:30:12 |
| 1 | user2 | system | hello | 2022-06-30 16:30:12 |
| 2 | user1 | system | world | 2022-06-30 16:31:19 |
| 2 | user3 | system | world | 2022-06-30 16:31:19 |
| 3 | user3 | user1 | java | 2022-06-30 16:31:22 |
| 3 | user4 | user1 | java | 2022-06-30 16:31:22 |
| 3 | user5 | user1 | java | 2022-06-30 16:31:22 |
| 4 | user7 | user2 | neovim | 2022-06-30 16:31:53 |
| 4 | user8 | user2 | neovim | 2022-06-30 16:31:53 |
| 5 | itkey | system | itkey | 2022-06-30 16:32:17 |
| 5 | lxyoucan | system | itkey | 2022-06-30 16:32:17 |
| 6 | user | good | very | 2022-06-30 16:38:18 |
| 6 | user11 | good | very | 2022-06-30 16:38:18 |
After getting this watch , The query is relatively simple . I'm more worried about , The performance may not be particularly high .
At this time, we still press user To query , It is written as follows :
select * from (
SELECT message_id,
substring_index(substring_index(msg.receiver, ',', topic.help_topic_id + 1), ',',
- 1) AS receiver,
sender,
message,
create_time
FROM t_message msg
JOIN mysql.help_topic topic ON topic.help_topic_id <
(length(msg.receiver) - length(REPLACE(msg.receiver, ',', '')) + 1)
) as tmsg where tmsg.receiver='user';
| message_id | receiver | sender | message | create_time |
|---|---|---|---|---|
| 6 | user | good | very | 2022-06-30 16:38:18 |
The result of this query is correct .
Reduce the use of subqueries , It can be written as follows :
SELECT
message_id,
sender,
message,
create_time
FROM
t_message msg
JOIN mysql.help_topic topic ON topic.help_topic_id < ( length( msg.receiver ) - length( REPLACE ( msg.receiver, ',', '' ) ) + 1 )
where substring_index( substring_index( msg.receiver, ',', topic.help_topic_id + 1 ), ',',- 1 ) = 'user';
Query results :
| message_id | sender | message | create_time |
|---|---|---|---|
| 6 | good | very | 2022-06-30 16:38:18 |
summary
The accuracy of the final plan should be no problem , But there is still room for optimization in terms of performance .
Reference resources
边栏推荐
- Design and implementation of kubelet garbage collection mechanism to protect nodes from being preempted by containers image GC high threshold
- 2022 beautician (intermediate) new version test questions and beautician (intermediate) certificate examination
- Download and install captura and configure ffmpeg in captura
- 在 .NET 6 项目中使用 Startup.cs
- How to connect WiFi with raspberry pie
- Mila, University of Ottawa | molecular geometry pre training with Se (3) invariant denoising distance matching
- [Blue Bridge Road - bug free code] pcf8591 - code analysis of AD conversion
- Wechat applet + Alibaba IOT platform + Hezhou air724ug build a serverless IOT system (III) -- wechat applet is directly connected to Alibaba IOT platform aliiot
- Commands related to the startup of redis under Linux server (installation and configuration)
- pytorch开源吗?
猜你喜欢

2022 tea master (primary) examination questions and tea master (primary) examination question bank

Deep dive kotlin synergy (19): flow overview

Nat. Comm. | use tensor cell2cell to deconvolute cell communication with environmental awareness

SAP ui5 application development tutorial 105 - detailed introduction to the linkage effect implementation of SAP ui5 master detail layout mode

在 .NET 6 项目中使用 Startup.cs

有监督预训练!文本生成又一探索!

Appium自动化测试框架

JS realizes the animation effect of text and pictures in the visual area
![[brush questions] most elements (super water king problem)](/img/79/13a715b74bc18a4a62113de76a65f6.png)
[brush questions] most elements (super water king problem)

Error in compiled file: error: unmapped character encoding GBK
随机推荐
Half of 2022 is over, so we must hurry up
Web session management security issues
[no title] 2022 chlorination process examination content and free chlorination process examination questions
sklearn数据预处理
SAP ui5 application development tutorial 105 - detailed introduction to the linkage effect implementation of SAP ui5 master detail layout mode
2022 beautician (intermediate) new version test questions and beautician (intermediate) certificate examination
How to process the current cell with a custom formula in conditional format- How to address the current cell in conditional format custom formula?
服务器无法远程连接原因分析
Without sxid, suid & sgid will be in danger- Shangwen network xUP Nange
Introduction to eth
Read a paper_ ChineseBert
Nat. Comm. | use tensor cell2cell to deconvolute cell communication with environmental awareness
Deep dive kotlin synergy (19): flow overview
The longest subarray length with a positive product of 1567 recorded by leecode
2022 P cylinder filling examination content and P cylinder filling practice examination video
Sklearn data preprocessing
The latest analysis of the main principals of hazardous chemical business units in 2022 and the simulated examination questions of the main principals of hazardous chemical business units
CVPR 2022 | Dalian Technology propose un cadre d'éclairage auto - étalonné pour l'amélioration de l'image de faible luminosité de la scène réelle
CVPR 2022 | 大连理工提出自校准照明框架,用于现实场景的微光图像增强
阿洛对自己的思考