当前位置:网站首页>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
边栏推荐
- [Blue Bridge Road - bug free code] pcf8591 - code analysis of AD conversion
- "Final review" 16/32-bit microprocessor (8086) basic register
- 2.14 simulation summary
- 毕设-基于SSM宠物领养中心
- 在写web项目的时候,文件上传用到了smartupload,用了new string()进行转码,但是在数据库中,还是会出现类似扑克的乱码
- CVPR 2022 | 大連理工提出自校准照明框架,用於現實場景的微光圖像增强
- Five elements of user experience
- 2022年已过半,得抓紧
- js/ts底层实现双击事件
- leetcode:297. 二叉树的序列化与反序列化
猜你喜欢

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

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

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

JS native common knowledge

Makefile demo

Nodejs Foundation: shallow chat URL and querystring module

国产PC系统完成闭环,替代美国软硬件体系的时刻已经到来

CVPR 2022 | 大连理工提出自校准照明框架,用于现实场景的微光图像增强

第十届中国云计算大会·中国站:展望未来十年科技走向

Error in compiled file: error: unmapped character encoding GBK
随机推荐
类的基础语法
CVPR 2022 | 大连理工提出自校准照明框架,用于现实场景的微光图像增强
Supervised pre training! Another exploration of text generation!
Esp32 series (3): GPIO learning (take simple GPIO input and output, ADC, DAC as examples)
2022 tea master (intermediate) examination questions and analysis and tea master (intermediate) practical examination video
105. SAP UI5 Master-Detail 布局模式的联动效果实现明细介绍
【毕业季·进击的技术er】职场人的自白
Sklearn data preprocessing
[no title] 2022 chlorination process examination content and free chlorination process examination questions
Nat. Comm. | use tensor cell2cell to deconvolute cell communication with environmental awareness
[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
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
2022 beautician (intermediate) new version test questions and beautician (intermediate) certificate examination
China Mobile Internet of things oneos and onenet were selected in the list of 2021 Internet of things demonstration projects
中移物联网OneOS与OneNET入选《2021年物联网示范项目名单》
学会pytorch能干什么?
js实现在可视区内,文字图片动画效果
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?
pytorch怎么下载?pytorch在哪里下载?
Mutex and rwmutex in golang