[英]MySQL - List results sort by same values first ?
I want to get the rows firstly displayed which have the same row data with the country (There is a row that named country). 我想首先显示与国家/地区具有相同行数据的行(有一个名为国家/地区的行)。 For example if there are 5 results and 2 of them have the country of "England", they should came first.
例如,如果有5个结果,而其中2个的国家为“ England”,则应排在第一位。 I have tried
我努力了
$mycountry = "england";
mysql_query("SELECT * FROM members where basresvar='yes' ORDER BY country DESC limit 10")
But it doesnt sort the results as I want. 但这并没有按照我的意愿对结果进行排序。 What is the correct way to do that ?
正确的做法是什么?
You could write your query this way: 您可以通过以下方式编写查询:
select *
from
members
where
basresvar='yes'
order by
case when country='england' then 1 else 2 end
limit 10
the case...when
statement will return 1 when the country='england' and 2 otherwise, then since we are ordering by this statement this will put rows that have country='england' at the top, and others at the bottom. case...when
语句在country ='england' case...when
将返回1,否则返回2,则由于我们正在按此语句进行排序,因此将把具有country ='england'的行放在顶部,将其他行放在底部。
Use 采用
ORDER BY country = "England" DESC, country
When a comparison is true, the value is 1
; 比较为真时,值为
1
; when it's false the value is 0
. 如果为假,则值为
0
。 So when you order by this comparison in descending order, the rows where the country is equal to England will be put first. 因此,当您通过此比较以降序排列时,国家/地区等于英国的行将放在第一位。
Adding a second ordering criteria makes all the remaining countries sort alphabetically after England. 添加第二个排序条件会使所有其他国家/地区按字母顺序在英格兰之后排序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.