当前位置:网站首页>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
边栏推荐
- Js/ts bottom implementation double click event
- Recursion: one dimensional linked lists and arrays
- Export of zip file
- Wechat applet + Alibaba IOT platform + Hezhou air724ug build a serverless IOT system (III) -- wechat applet is directly connected to Alibaba IOT platform aliiot
- 2022-07-02: what is the output of the following go language code? A: Compilation error; B:Panic; C:NaN。 package main import “fmt“ func main() { var a =
- How to connect WiFi with raspberry pie
- 2022 tea master (intermediate) examination questions and analysis and tea master (intermediate) practical examination video
- [set theory] set concept and relationship (set family | set family examples | multiple sets)
- 2022 polymerization process examination questions and polymerization process examination skills
- Nat. Comm. | use tensor cell2cell to deconvolute cell communication with environmental awareness
猜你喜欢
![[home push IMessage] software installation virtual host rental tothebuddy delay](/img/e7/eb20a773e4b674962f856d179a3769.jpg)
[home push IMessage] software installation virtual host rental tothebuddy delay

pytorch是什么?pytorch是一个软件吗?

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

Nat. Comm. | 使用Tensor-cell2cell对细胞通讯进行环境感知去卷积

中移物联网OneOS与OneNET入选《2021年物联网示范项目名单》

用户体验五要素

2022 Shandong Province safety officer C certificate examination questions and Shandong Province safety officer C certificate simulation examination question bank

Mutex and rwmutex in golang

105. Detailed introduction of linkage effect realization of SAP ui5 master detail layout mode

Is it better to speculate in the short term or the medium and long term? Comparative analysis of differences
随机推荐
Nat. Comm. | use tensor cell2cell to deconvolute cell communication with environmental awareness
Commands related to the startup of redis under Linux server (installation and configuration)
How does the pytorch project run?
Debug: CD cannot be used in kaggle
Social phobia of contemporary young people (III)
8.8.2-PointersOnC-20220214
CVPR 2022 | 大连理工提出自校准照明框架,用于现实场景的微光图像增强
Is pytorch difficult to learn? How to learn pytorch well?
JMeter starts from zero (III) -- simple use of regular expressions
2022 polymerization process examination questions and polymerization process examination skills
Nodejs Foundation: shallow chat URL and querystring module
Error in compiled file: error: unmapped character encoding GBK
[mathematical logic] predicate logic (judge whether the first-order predicate logic formula is true or false | explain | example | predicate logic formula type | forever true | forever false | satisfi
golang xxx. Go code template
What can learning pytorch do?
[mathematical logic] predicate logic (toe normal form | toe normal form conversion method | basic equivalence of predicate logic | name changing rules | predicate logic reasoning law)
Taking two column waterfall flow as an example, how should we build an array of each column
『期末复习』16/32位微处理器(8086)基本寄存器
【刷题篇】接雨水(一维)
Appium自动化测试框架