[mysql] mysql alphabetical order

i am trying to sort mysql data with alphabeticaly like

A | B | C | D

when i click on B this query runs

select name from user order by 'b'

but result showing all records starting with a or c or d i want to show records only starting with b

thanks for help

This question is related to mysql

The answer is


MySQL solution:

select Name from Employee order by Name ;

Order by will order the names from a to z.


You can use:

SELECT name FROM user WHERE name like 'b%' ORDER BY name

but result showing all records starting with a or c or d i want to show records only starting with b

You should use WHERE in that case:

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:

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:

select name from user where name like '%b%' order by name

You do not need to user where clause while ordering the data alphabetically. here is my code

SELECT * FROM tbl_name ORDER BY field_name

that's it. It return the data in alphabetical order ie; From A to Z. :)


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

select name from user where name like 'b%'

Wildcard Characters are used with like clause to sort records.

if we want to search a string which is starts with B then code is like the following:

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 ;

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 ;

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 ;

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 ;

note : one underscore for one character.


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%'