MySQL query order by multiple items

MysqlSql Order-By

Mysql Problem Overview


Is it possible to order by multiple rows?

I want my users to be sorted by last_activity, but at the same time, I want the users with pictures to appear before the ones without

Something like this:

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

Mysql Solutions


Solution 1 - Mysql

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

>Note that we can place ASC or DESC after each column (like above does for pic_set), or leave it to default (like above's last_activity-column).

Solution 2 - Mysql

Sort by picture and then by activity:

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

Solution 3 - Mysql

SELECT id, user_id, video_name
FROM sa_created_videos
ORDER BY LENGTH(id) ASC, LENGTH(user_id) DESC

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlexanderView Question on Stackoverflow
Solution 1 - MysqlihorkoView Answer on Stackoverflow
Solution 2 - MysqleumiroView Answer on Stackoverflow
Solution 3 - MysqlPankaj YadavView Answer on Stackoverflow