MySQL查询顺序由多个项目


95

可以按多行订购吗?

我希望我的用户按last_activity进行排序,但同时,我希望带有图片的用户出现在没有图片的用户之前

像这样:

SELECT some_cols
FROM `prefix_users`
WHERE (some conditions)
ORDER BY last_activity, pic_set DESC;

2
图片在哪里定义?
eumiro'2

1
给其他人的提示。如果您在'last_activity'上取消了ASC / DESC,则默认排序顺序可以是ASC。
Geekygecko

Answers:


137
SELECT some_cols
FROM prefix_users
WHERE (some conditions)
ORDER BY pic_set DESC, last_activity;

14

按图片排序,然后按活动排序:

SELECT some_cols
FROM `prefix_users`
WHERE (some conditions)
ORDER BY pic_set, last_activity DESC;

2
我不明白,DESC去哪了?每个项目后面?
亚历山大

2
是。您可能想订购last_activity最新到最旧的产品。而且,如果您解释图片的定义方式(pic_set代表什么意思),我也可以告诉您如何对该属性进行排序。
eumiro'2

5
亚历山大,我相信你能做到ORDER BY pic_set DESC, last_activity DESC或者ORDER BY pic_set DESC, last_activity ASC为每列,默认排序方向在MySQL(默认配置),因此排序方向是ASC。
ZurabWeb 2014年

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.