MySQL: Usage of 'REGEXP' instead of 'FIND_IN_SET'

社会主义新天地 提交于 2019-12-25 01:45:03

问题


I am having limitation in using MySQL's FIND_IN_SET function for searching array in set. Hence thinking of using of REGEXP. However can anyone help me constructing it.

E.g. My requirement

SELECT * FROM table AS t WHERE FIND_IN_SET('1,2,3', t.list);

Hence thinking of using REGEXP function to search array within set.

SELECT * FROM table AS t WHERE t.list REGEXP '1,2,3';

Can anyone help me building this REGEXP.


回答1:


You can do like this:

SELECT * FROM table AS t WHERE t.list REGEXP '^9,|,9$|,9,' OR t.list =9



回答2:


You can split your search string and continue to use FIND_IN_SET()

SELECT * 
  FROM `table` AS t 
 WHERE FIND_IN_SET('1', t.list)
   AND FIND_IN_SET('2', t.list)
   AND FIND_IN_SET('3', t.list)

Better yet normalize your data by introducing a many-to-many table.




回答3:


For your requirements you can easily use:

SELECT * 
FROM table1 AS t 
WHERE t.list REGEXP '1|2|3';

To learn about regular expressions take look at this software: http://www.weitz.de/regex-coach/




回答4:


try the fallowing sql statement:

SELECT * 
FROM table AS t 
WHERE t.list REGEXP '^(1$|2$|3$)';


来源:https://stackoverflow.com/questions/21130690/mysql-usage-of-regexp-instead-of-find-in-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!