使用 mysql 查询查找所有以字母 'a' 开头的名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1951320/
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 14:51:51  来源:igfitidea点击:

find all the name using mysql query which start with the letter 'a'

mysql

提问by Salil

I want to find the data from table artists where name is start with letter a, b, c.

我想从表格艺术家那里找到名称以字母 a、b、c 开头的数据。

i.e. 
My o/p should contain
Adam
Alan
Bob
Ben
Chris
Cameron

But not GlAin, doC, dolBie

采纳答案by Dienow

You can use like 'A%' expression, but if you want this query to run fast for large tables I'd recommend you to put number of first button into separate field with tiny int type.

您可以使用 like 'A%' 表达式,但如果您希望此查询在大表上快速运行,我建议您将第一个按钮的数量放入带有小 int 类型的单独字段中。

回答by mike

In MySQL use the '^'to identify you want to check the first char of the string then define the array [] of letters you want to check for.

在 MySQL 中,使用'^'来标识要检查字符串的第一个字符,然后定义要检查的字母数组 []。

Try This

尝试这个

SELECT * FROM artists WHERE name REGEXP '^[abc]'

回答by Dumb Guy

Try this:

尝试这个:

select * from artists where name like "A%" or name like "B%" or name like "C%"

回答by madhu131313

One can also use RLIKEas below

一个也可以使用RLIKE如下

SELECT * FROM artists WHERE name RLIKE '^[abc]';

回答by Mohammed Jafar

Try this simple select:

试试这个简单的选择:

select * 
from artists 
where name like "a%"

回答by akostre

I would go for substr() functionality in MySql.

我会在 MySql 中使用 substr() 功能。

Basically, this function takes account of three parameters i.e. substr(str,pos,len)

基本上,这个函数考虑了三个参数,即 substr(str,pos,len)

http://www.w3resource.com/mysql/string-functions/mysql-substr-function.php

http://www.w3resource.com/mysql/string-functions/mysql-substr-function.php

SELECT * FROM artists 
WHERE lower(substr(name,1,1)) in ('a','b','c');

回答by curious visitor

try using CHARLISTas shown below:

尝试使用CHARLIST,如下所示:

select distinct name from artists where name RLIKE '^[abc]';

use distinct only if you want distinct values only. To read about it Click here.

仅当您只想要不同的值时才使用 distinct 。要阅读有关它,请单击此处