开发者

MySQL Regexp for Finding a CSV number

开发者 https://www.devze.com 2023-02-04 13:15 出处:网络
How do you find a number (e.g., \"8\") in a CSV string in a MySQL field without finding numbers that contain that number (e.g., \"18\" or \"81\")?

How do you find a number (e.g., "8") in a CSV string in a MySQL field without finding numbers that contain that number (e.g., "18" or "81")?

For example:

  1. csvString1 in somecolumn = 8,14,18
  2. csvString2 in somecolumn = 4,5,8,13
  3. csvString2 in somecolumn = 18,81,82,88

I need to have #1 and #2 come up true and #3 come up false. How do I do that

SELECT * FROM sometable WHERE s开发者_开发知识库omecolumn REGEXP '(what here?)';


use find_in_set

select find_in_set('8', '18,81,82,88');
--> zero

select find_in_set('8', '8,14,18');
--> 1

select find_in_set('8', '4,5,8,13');
--> 3

So,

select * from your_table
where find_in_set('8', your_col)<>0;

PS: normalize your data to avoid future problem

0

精彩评论

暂无评论...
验证码 换一张
取 消