gpt4 book ai didi

MySQL 查询,检查单位数和两位数

转载 作者:行者123 更新时间:2023-11-30 00:25:28 24 4
gpt4 key购买 nike

我的客户提供的数据库中有一个列,其中包含诸如“2;”之类的值。 3; 14'或'1'等。我正在使用MySQL。如何编写查询以便

1) 我可以检查该列是否包含数字(例如 1)2) 例如,如果我检查“1”而该值实际上是“14”,则不会得到“命中”。

先谢谢了

最佳答案

如果列是 varchar 并且您想在“1;3;14”中搜索“1”时返回行,则可以使用 REGEXP 运算符进行正则表达式搜索 word boundary character .

select * from test
where col regexp '[[:<:]]1[[:>:]]'

SQL FIddle Demo

来自 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

关于MySQL 查询,检查单位数和两位数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22927316/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com