mysql 字母顺序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3968135/
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
mysql alphabetical order
提问by r1400304
i am trying to sort mysql data with alphabeticaly like
我正在尝试按字母顺序对 mysql 数据进行排序,例如
A | B | C | D
一个 | 乙 | C | D
when i click on B this query runs
当我点击 B 这个查询运行
select name from user order by 'b'
按'b'从用户顺序中选择名称
but result showing all records starting with a or c or d i want to show records only starting with b
但结果显示所有以 a 或 c 或 di 开头的记录只想显示以 b 开头的记录
thanks for help
感谢帮助
回答by MatTheCat
i want to show records only starting with b
我想显示只以 b 开头的记录
select name from user where name LIKE 'b%';
i am trying to sort MySQL data alphabeticaly
我正在尝试按字母顺序对 MySQL 数据进行排序
select name from user ORDER BY name;
i am trying to sort MySQL data in reverse alphabetic order
我正在尝试按逆字母顺序对 MySQL 数据进行排序
select name from user ORDER BY name desc;
回答by Sarfraz
but result showing all records starting with a or c or d i want to show records only starting with b
但结果显示所有以 a 或 c 或 di 开头的记录只想显示以 b 开头的记录
You should use WHERE
in that case:
WHERE
在这种情况下你应该使用:
select name from user where name = 'b' order by name
If you want to allow regex, you can use the LIKE
operator there too if you want. Example:
如果你想允许正则表达式,你也可以在LIKE
那里使用运算符。例子:
select name from user where name like 'b%' order by name
That will select records starting with b
. Following query on the other hand will select all rows where b
is found anywhere in the column:
这将选择以b
. 另一方面,以下查询将选择b
在列中任何位置找到的所有行:
select name from user where name like '%b%' order by name
回答by codaddict
You can use:
您可以使用:
SELECT name FROM user WHERE name like 'b%' ORDER BY name
回答by user3020691
You do not need to user where clause while ordering the data alphabetically. here is my code
按字母顺序排列数据时,您不需要使用 where 子句。这是我的代码
SELECT * FROM tbl_name ORDER BY field_name
that's it. It return the data in alphabetical order ie; From A to Z. :)
就是这样。它按字母顺序返回数据,即;从A到Z。 :)
回答by Laxman Panigrahi
Wildcard Characters are used with like clause to sort records.
通配符与like 子句一起用于对记录进行排序。
if we want to search a string which is starts with B then code is like the following:
如果我们要搜索以 B 开头的字符串,则代码如下:
select * from tablename where colname like 'B%' order by columnname ;
select * from tablename where colname like 'B%' order by columnname ;
if we want to search a string which is ends with B then code is like the following: select * from tablename where colname like '%B' order by columnname ;
如果我们要搜索以 B 结尾的字符串,则代码如下: select * from tablename where colname like '%B' order by columnname ;
if we want to search a string which is contains B then code is like the following: select * from tablename where colname like '%B%' order by columnname ;
如果我们要搜索包含 B 的字符串,则代码如下所示: select * from tablename where colname like '%B%' order by columnname ;
if we want to search a string in which second character is B then code is like the following: select * from tablename where colname like '_B%' order by columnname ;
如果我们要搜索第二个字符为 B 的字符串,则代码如下所示: select * from tablename where colname like '_B%' order by columnname ;
if we want to search a string in which third character is B then code is like the following: select * from tablename where colname like '__B%' order by columnname ;
如果我们要搜索第三个字符为 B 的字符串,则代码如下所示: select * from tablename where colname like '__B%' order by columnname ;
note : one underscore for one character.
注意:一个下划线代表一个字符。
回答by Hammerite
If you want to restrict the rows that are returned by a query, you need to use a WHERE
clause, rather than an ORDER BY
clause. Try
如果要限制查询返回的行,则需要使用WHERE
子句,而不是ORDER BY
子句。尝试
select name from user where name like 'b%'
回答by Abhay Singh
I try to sort data with query it working fine for me please try this:
我尝试使用查询对数据进行排序,它对我来说工作正常,请尝试以下操作:
select name from user order by name asc
Also try below query for search record by alphabetically
还可以尝试以下按字母顺序查询搜索记录
SELECT name FROM `user` WHERE `name` LIKE 'b%'