0

I have the following mysql statement

SELECT login, personal_email 
FROM users 
WHERE personal_email in ('cc', 'bb', 'dd', 'aa')

I want the output in the following order.

+-------------------------------------+-------------------------------+
| login                               | personal_email                |
+-------------------------------------+-------------------------------+
   xx                                     cc
   yy                                     bb
   zz                                     dd
   ll                                     aa

But I am getting output in random order, like below.

+-------------------------------------+-------------------------------+
| login                               | personal_email                |
+-------------------------------------+-------------------------------
   ll                                     aa
   zz                                     dd
   xx                                     cc
   yy                                     bb

How do I get the results in proper order.

Goku__
  • 940
  • 1
  • 12
  • 25

3 Answers3

1

If it is alphabetical order that you want, you might want to have a look at ORDER BY clause. It will sort your results according to the alphabetical (or reverse alphabetical, depending on ASC or DSC/DESC).

Try this

ORDER BY personal_email
Kaushik
  • 2,072
  • 1
  • 23
  • 31
pri
  • 1,521
  • 2
  • 13
  • 26
1

You can sort alphabetical using order by clause. Try below query:

select login, personal_email
from users
where personal_email in ('aa', 'bb', 'cc', 'dd')
order by login
vivekpansara
  • 895
  • 1
  • 6
  • 14
0

You need to use the order by command. This takes one argument, ASC or DESC for ascending or descending order. Ascending order is A-Z / 1-9, descending order is Z-A / 9-1 etc. The default is asc - that is, if neither asc or desc appears after the order by, it defaults to asc

SELECT a,b FROM foo ORDER BY a

user3791372
  • 4,445
  • 6
  • 44
  • 78