MySQL MySQL正则表达式查询不区分大小写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3993272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 17:31:32  来源:igfitidea点击:

MySQL regex query case insensitive

mysql

提问by Bharanikumar

In my table I have firstname and last name. Few names are upper case ( ABRAHAM ), few names are lower case (abraham), few names are character starting with ucword (Abraham).

在我的表中,我有名字和姓氏。很少有名字是大写的(ABRAHAM),很少有名字是小写的(abraham),很少有名字是以 ucword 开头的字符(Abraham)。

So when i am doing the where condition using REGEXP '^[abc]', I am not getting proper records. How to change the names to lower case and use SELECT QUERY.

因此,当我使用 REGEXP '^[abc]' 执行 where 条件时,我没有得到正确的记录。如何将名称更改为小写并使用 SELECT QUERY。

SELECT * FROM `test_tbl` WHERE cus_name REGEXP '^[abc]';

This is my query, works fine if the records are lower case, but my records are intermediate ,my all cus name are not lower case , all the names are like ucword.

这是我的查询,如果记录是小写就可以正常工作,但我的记录是中间的,我所有的 cus 名称都不是小写,所有的名称都像 ucword。

So for this above query am not getting proper records display.

所以对于上面的查询,没有得到正确的记录显示。

回答by David Conde

I think you should query your database making sure that the names are lowered, suppose that name is the name you whish to find out, and in your application you've lowered it like 'abraham', now your query should be like this:

我认为您应该查询您的数据库以确保名称被降低,假设该名称是您想要查找的名称,并且在您的应用程序中您已经将其降低为“abraham”,现在您的查询应该是这样的:

SELECT * FROM `test_tbl` WHERE LOWER(cus_name) = name

Since i dont know what language you use, I've just placed name, but make sure that this is lowered and you should retrieve Abraham, ABRAHAM or any variation of the name!

由于我不知道您使用的是什么语言,因此我刚刚放置了名称,但请确保将其降低,并且您应该检索 Abraham、ABRAHAM 或名称的任何变体!

Hepe it helps!

Hepe它有帮助!

回答by codaddict

Have you tried:

你有没有尝试过:

SELECT * FROM `test_tbl` WHERE LOWER(cus_name) REGEXP '^[abc]';

回答by aeon

You don't need regexp to search for names starting with a specific string or character.

您不需要正则表达式来搜索以特定字符串或字符开头的名称。

SELECT * FROM `test_tbl` WHERE cus_name LIKE 'abc%' ; 

% is wildcard char. The search is case insensitive unless you set the binary attribute for column cus_name or you use the binary operator

% 是通配符。搜索不区分大小写,除非您为列 cus_name 设置 binary 属性或使用二元运算符

SELECT * FROM `test_tbl` WHERE BINARY cus_name LIKE 'abc%' ; 

回答by Cragmonkey

A few valid options already presented, but here's one more with just regex:

已经提供了一些有效的选项,但这里还有一个只有正则表达式的选项:

SELECT * FROM `test_tbl` WHERE cus_name REGEXP '^[abcABC]';