开发者

Order mysql fetched data by the same content of a field

开发者 https://www.devze.com 2023-01-20 03:39 出处:网络
i have a database with: city / age for example: Chicago 24 York 33 Chicago 54 London 12 York 21 London 1 How can i oreder thing like this?

i have a database with: city / age for example: Chicago 24 York 33 Chicago 54 London 12 York 21 London 1

How can i oreder thing like this? Chicago 24 Chicago 54 Yor开发者_StackOverflowk 33 York 21 London 1 London 12

Basically order them by the name of the town. I use this for a php display records script.

mysql_query("SELECT * FROM towns WHERE .........");


To change the order of the results you use ORDER BY, not WHERE.

SELECT city, age
FROM towns
ORDER BY city

If you want the cities in the order in your example then use FIELD:

SELECT city, age
FROM towns
ORDER BY FIELD(city, 'Chicago', 'York', 'London')


Try

SELECT city, age FROM towns ORDER BY city DESC, age;

That will order by city DESC first and then age [ASC] second... which is what I believe you require.

0

精彩评论

暂无评论...
验证码 换一张
取 消