当前位置:网站首页>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
边栏推荐
- pytorch难学吗?如何学好pytorch?
- TCP, the heavyweight guest in tcp/ip model -- Kuige of Shangwen network
- Mutex and rwmutex in golang
- MySQL timestampdiff interval
- Arlo's thinking about himself
- 2022-07-02:以下go语言代码输出什么?A:编译错误;B:Panic;C:NaN。 package main import “fmt“ func main() { var a =
- [brush questions] connected with rainwater (one dimension)
- ZIP文件的导出
- 2022 tea master (intermediate) examination questions and analysis and tea master (intermediate) practical examination video
- [national programming] [software programming - Lecture Video] [zero foundation introduction to practical application]
猜你喜欢

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

因果AI,下一代可信AI的产业升级新范式?

Cnopendata China Customs Statistics

2022-07-02:以下go语言代码输出什么?A:编译错误;B:Panic;C:NaN。 package main import “fmt“ func main() { var a =

Esp32 series (3): GPIO learning (take simple GPIO input and output, ADC, DAC as examples)

IPv6 foundation construction experiment

pytorch开源吗?

JS realizes the animation effect of text and pictures in the visual area

2022 tea master (intermediate) examination questions and analysis and tea master (intermediate) practical examination video

Message queue addition failure
随机推荐
Appium automated testing framework
CVPR 2022 | 大连理工提出自校准照明框架,用于现实场景的微光图像增强
Analysis of the reason why the server cannot connect remotely
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
[Apple Photo Album push] IMessage group anchor local push
阿洛对自己的思考
Esp32 series (3): GPIO learning (take simple GPIO input and output, ADC, DAC as examples)
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
Competitive product analysis and writing
"Final review" 16/32-bit microprocessor (8086) basic register
What is the correct way to compare ntext columns with constant values- What's the right way to compare an NTEXT column with a constant value?
2022 tea master (primary) examination questions and tea master (primary) examination question bank
Makefile demo
2022 polymerization process examination questions and polymerization process examination skills
Introduction to eth
JMeter starts from zero (III) -- simple use of regular expressions
TCP, the heavyweight guest in tcp/ip model -- Kuige of Shangwen network
Export of zip file
C language hashtable/hashset library summary
2022 tea master (intermediate) examination questions and analysis and tea master (intermediate) practical examination video