gpt4 book ai didi

使用 CONCAT 时 MySQL REGEXP 区分大小写

转载 作者:太空宇宙 更新时间:2023-11-03 11:40:16 26 4
gpt4 key购买 nike

通常 REGEXP 不区分大小写,例如:

mysql> select count(*) from data_tool where name regexp 'CD';
+----------+
| count(*) |
+----------+
| 22 |
+----------+
1 row in set (0.00 sec)

mysql> select count(*) from data_tool where name regexp 'cd';
+----------+
| count(*) |
+----------+
| 22 |
+----------+
1 row in set (0.00 sec)

但似乎当我在 concat 中使用时它变得区分大小写,例如:

mysql> select count(*) from data_tool where concat_ws('.',name,id) regexp 'cd';
+----------+
| count(*) |
+----------+
| 0 |
+----------+
1 row in set (0.00 sec)

mysql> select count(*) from data_tool where concat_ws('.',name,id) regexp 'CD';
+----------+
| count(*) |
+----------+
| 22 |
+----------+
1 row in set (0.00 sec)

这是怎么回事?

最佳答案

manual says :

REGEXP is not case sensitive, except when used with binary strings.

所以您的 CONCAT 字符串之一是二进制的。

因此,如果它适用于您的 name 列,这意味着该列是标准字符串 (_ci),但随后附加了 id假设的列是一个数值,这是一个二进制列,因此导致 REGEXP 区分大小写。

解决方案:

but in the real case I am concating a char and an int.

CAST您的数字列作为不区分大小写的字符串:

 SELECT count(*) FROM data_tool WHERE 
concat_ws('.',name,CAST(id AS CHAR)) REGEXP 'cd';

可能还需要仔细检查您的默认表排序规则是否以 _ci 结尾,表明默认字符串列值不区分大小写。 < br/>请注意,您不能Int 转换为 Varchar 类型。

希望对您有所帮助。

如果没有,请编辑您的问题并输出您的表结构详细信息(SHOW CREATE TABLE),以便我们了解它是如何制作的。干杯

关于使用 CONCAT 时 MySQL REGEXP 区分大小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42422543/

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