将某些值排序到顶部 [英] Sort certain values to the top
本文介绍了将某些值排序到顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含以下数据(简化)的MySQL表:
I have a MySQL table with the following data (simplified):
INSERT INTO `stores` (`storeId`, `name`, `country`) VALUES
(1, 'Foo', 'us'),
(2, 'Bar', 'jp'),
(3, 'Baz', 'us'),
(4, 'Foo2', 'se'),
(5, 'Baz2', 'jp'),
(6, 'Bar3', 'jp');
现在,我希望能够获得以客户所在国家/地区开头的分页商店列表.
Now, I want to be able to get a paginated list of stores that begins with the customers country.
例如,一个美国客户将看到以下列表:
For example, an american customer would see the following list:
Foo
Baz
Bar
Foo2
Baz2
Bar3
我现在正在使用的天真的解决方案(例如,有一个美国客户,页面大小为3):
The naive solution I'm using right now (example with an american customer and page size 3):
(SELECT * FROM stores WHERE country = "us") UNION (SELECT * FROM stores WHERE country != "us") LIMIT 0,3
有没有更好的方法可以做到这一点?可以使用ORDER BY
并让其将某个值放在顶部吗?
Are there any better ways of doing this? Could ORDER BY
be used and told to put a certain value at the top?
推荐答案
尝试一下:
SELECT * FROM stores ORDER BY country = "us" DESC, storeId
这篇关于将某些值排序到顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文