我在客户端提供的数据库中有一个列,其值为'2; 3; 14'或'1'等我正在使用MySQL。如何编写查询以便
1)我可以检查列是否包含数字(例如1) 2)例如,如果我检查'1'并且值实际为'14',我将不会得到'命中'。
谢谢是提前
答案 0 :(得分:0)
如果列是varchar,并且您希望在搜索' 1'时返回行。 in' 1; 3; 14'然后,您可以使用REGEXP运算符进行word boundary character的正则表达式搜索。
select * from test
where col regexp '[[:<:]]1[[:>:]]'
来自MySQL文档
Word Boundary Markers [[:<:]], [[:>:]]
These markers stand for word boundaries.
They match the beginning and end of words, respectively.
A word is a sequence of word characters that is not preceded by or followed by word characters.
A word character is an alphanumeric character in the alnum class or an underscore (_).
mysql> SELECT 'a word a' REGEXP '[[:<:]]word[[:>:]]'; -> 1
mysql> SELECT 'a xword a' REGEXP '[[:<:]]word[[:>:]]'; -> 0