当前位置:网站首页>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_index
The 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_topic
It's the databasemysql
A 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
边栏推荐
- Web session management security issues
- What is pytorch? Is pytorch a software?
- [Apple Photo Album push] IMessage group anchor local push
- When writing a web project, SmartUpload is used for file upload and new string () is used for transcoding, but in the database, there will still be random codes similar to poker
- [brush questions] most elements (super water king problem)
- [Blue Bridge Road -- bug free code] interpretation of some codes of matrix keyboard
- Deep dive kotlin synergy (20): build flow
- Wechat applet + Alibaba IOT platform + Hezhou air724ug built with server version system analysis
- MySQL timestampdiff interval
- Js/ts bottom implementation double click event
猜你喜欢
CEPH Shangwen network xUP Nange that releases the power of data
Causal AI, a new paradigm for industrial upgrading of the next generation of credible AI?
Application of I2C protocol of STM32F103 (read and write EEPROM)
2022 mobile crane driver examination registration and mobile crane driver operation examination question bank
What is pytorch? Is pytorch a software?
Esp32 series (3): GPIO learning (take simple GPIO input and output, ADC, DAC as examples)
Competitive product analysis and writing
Interaction free shell programming
Bisher - based on SSM pet adoption center
In Net 6 project using startup cs
随机推荐
JMeter starts from zero (III) -- simple use of regular expressions
105. SAP UI5 Master-Detail 布局模式的联动效果实现明细介绍
2022 tea master (intermediate) examination questions and analysis and tea master (intermediate) practical examination video
pytorch怎么下载?pytorch在哪里下载?
错误 C2694 “void Logger::log(nvinfer1::ILogger::Severity,const char *)”: 重写虚函数的限制性异常规范比基类虚成员函数
Is it better to speculate in the short term or the medium and long term? Comparative analysis of differences
Without sxid, suid & sgid will be in danger- Shangwen network xUP Nange
C language hashtable/hashset library summary
【刷题篇】多数元素(超级水王问题)
Social phobia of contemporary young people (II)
[Blue Bridge Road - bug free code] pcf8591 - code analysis of AD conversion
eth入门之DAPP
JS realizes lazy loading of pictures
深潜Kotlin协程(二十):构建 Flow
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
Analysis of the reason why the server cannot connect remotely
JS native common knowledge
[brush questions] find the number pair distance with the smallest K
学会pytorch能干什么?
2022 polymerization process examination questions and polymerization process examination skills