简体   繁体   English

SQL查询选择确切的列?

[英]SQL query to select columns with exact like?

Consider this SQL table 考虑这个SQL表

id | name | numbers
------------------------
1 | bob | 1 3 5
2 | joe | 7 2 15

This query returns the whole table as its result: 该查询返回整个表作为其结果:

SELECT * FROM table WHERE numbers LIKE '%5%'

Is there an SQL operator so that it only returns row 1 (only columns with the number 5)? 是否存在SQL运算符,使其仅返回第1行(仅返回数字5的列)?

Use regexp with word boundaries. 将正则regexp与单词边界一起使用。 (But you should ideally follow Gordon's comment) (但理想情况下,您应该遵循戈登的评论)

where numbers REGEXP '[[:<:]]5[[:>:]]'

遗憾的是您没有在numbers列中使用逗号作为分隔符,因为可以使用FIND_IN_SET函数,但可以将其与REPLACE一起使用,如下所示:

SELECT * FROM table WHERE FIND_IN_SET(5, REPLACE(numbers, ' ', ',')); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM