Mysql regular get the intersection of fields

The example is similar to Dingding clock attendance: SELECT * FROM fa_checkingroupWHERE ( (concat(',',cdepartment_id,',') regexp concat(',(',replace('2,12',',','|'), '),') OR (concat(',',cuser_id,',') regexp concat(',(',replace('40',',','|'),'),'))) AND !(concat(',',wuser_id,',') regexp concat(',(',replace('40',',','|'),'),')) ) AND company_id= 2

Problem Description 

For example, there are two records in table1 name no a 2,9 b 8,10 

Then there is a string of strings, which are 0, 1, 2, 3, 4 

Then through a sql, find out the records with no of 2, 9 to ```` 

Because there are 2 in the string, there are also 2 in the data 

Detailed explanation ----------------------------- The field of the table is name no a 2,9 b 8,10 The string is str ="0,1,2,3,4" The next step is to check the records that intersect with str in the no field. The result of the query is name=a, no=2,9--------- --------------------- 

Sql code 

select * from table1 where concat(',',no,',') regexp concat(',0,|,1,|,2,|,3,|,4,');  

or: 

Sql code 

select * from table1 where concat(',',no,',') regexp concat(',(',replace('0,1,2,3,4',',','|'),'),');  

The following is extended learning: 

For some reason, sometimes we don't follow the design guidelines of the paradigm and put some properties into the same string field. For example, personal interests, sometimes we design the table as create table members (uid int primary key, uname varchar(20), hobby varchar(100)); 

The contents of the table are as follows 

mysql> select * from members; +-----+-------+---------------------------- -----+ | uid | uname | hobby | +-----+-------+---------------------- -------------+ | 1 | AAAA | Music, Movies, Internet, Basketball, Reading, Table Tennis | | 2 | BBBB | Music, Reading, Table Tennis, Daze, Go, Zen | | 3 | CCCC | dating, table tennis | | 4 | DDDD | billiards, internet, reading, travel | | 5 | EEEE | ---------------------------------+ 4 rows in set (0.00 sec) 

What if we now want to find a member record with the same hobbies as a certain user X (reading, making friends, Go, football, skiing)? 

In other databases, we can only decompose the "reading, making friends, Go, football, skiing" strings into separate hobby items through programs or stored procedures, and then query them one by one like '%xxxx%'. But in MySQL we can directly use this regexp regular expression to construct SQL statements to achieve. 

First, we convert 'reading, making friends, Go, soccer, skiing' into a regular expression as 'reading | making friends | Go | football | skiing' , | means 'or' in the regular expression 

mysql> select replace('reading, making friends, Go, football, skiing',',','|'); +------------- ----------------------+ | replace('Reading, making friends, Go, football, skiing',',','|') | +-- -------------------------------------------------------+ | Reading | Dating | Go |Football|Skiing| +------------------------------------------------------ -+ 1 row in set (0.00 sec) 

This way we can use the SQL statement as follows. mysql> select * from members where hobby regexp replace('reading, making friends, Go, football, skiing',',','|'); +-----+-------+--- ------------------------------+ | uid | uname | hobby | +-----+----- --+---------------------------------+ | 1 | AAAA | music,movie,internet,basketball, reading, ping pong | | 2 | BBBB | music, reading, ping pong, daze, Go, Zen | | 3 | CCCC | making friends, table tennis | | 5 | EEEE | ---+-------+---------------------------------+ 3 rows in set (0.00 sec) 

With the above statement, we can get all the records of hobby including 'reading, making friends, Go, football, skiing' through a single SQL. 

But there is a small flaw in the above statement, that is, the item 'playing Go' is also selected. If it matches exactly, this record should not be selected. In order to avoid this situation, we make the following improvements to the SQL statement. 

Change the regular expression to ',(reading|friends|go|soccer|skiing),' that is, there must be a delimiter before and after the match "," 

mysql> select concat(',(',replace('reading, making friends, Go, football, skiing',',','|'),'),'); +---------- -------------------------------------------------- ---+ | concat(',(',replace('Reading, making friends, Go, football, skiing',',','|'),'),') | +-------- -------------------------------------------------- -----+ | ,(Reading|Friendship|Go|Football|Skiing), | +---------------------------- -----------------------------------+ 1 row in set (0.00 sec) 

mysql> select * from members -> where concat(',',hobby,',') regexp -> concat(',(',replace('reading, making friends, Go, football, skiing',',',' |'),'),'); +-----+-------+------------------------- --------+ | uid | uname | hobby | +-----+-------+------------------- --------------+ | 1 | AAAA | Music, Movies, Internet, Basketball, Reading, Ping Pong | | 2 | BBBB | Music, Reading, Ping Pong, Daze, Go, Zen | | 3 | CCCC | Dating, table tennis | +-----+-------+------------------------ ---------+ 3 rows in set (0.00 sec) 

This prevents the 5th record from being selected. 

Of course, this regular expression can also be used ',reading,|,friendship,|,Go,|,football,|,skiing,', but the efficiency is obviously not as good as ',(reading|friends|Go|football|skiing),' this planted. 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325241137&siteId=291194637