E
E
eldar_web2015-01-08 16:26:49
MySQL
eldar_web, 2015-01-08 16:26:49

How in MySQL (SQL) can you output (sort) in your own way?

There is a city table in the database:
id | name
1 | Rostov
2 | Moscow
3 | Krasnodar.
And how can you display in this order: Moscow, Rostov, Krasnodar without changing the id?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
S
Sergey Nalomenko, 2015-01-08
@nalomenko

Enter an additional pos field if you need to manually set the entry's unique location in the list. In this case, the id does not change, and you can set the order yourself:

id | pos | name
1  | 2   | Ростов
2  | 1   | Москва
3  | 3   | Краснодар

E
Eugene, 2015-01-08
@Nc_Soft

ORDER BY FIELD

D
Dmitry Bay, 2015-01-08
@kawabanga

If you need to sort once, then copy to an adjacent table with the correct sort.
If you need to sort by position, then use the first option above.

I
inververs, 2015-01-08
@inververs

ORDER BY FIELD(name, 'Rostov', 'Moscow', 'Krasnodar')

Y
Yuri, 2015-01-08
@error500

SELECT * FROM `table_name` ORDER BY name = 'Moscow' DESC, name DESC

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question