Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL query order by multiple items

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; 
like image 207
Alexander Avatar asked Feb 02 '11 15:02

Alexander


People also ask

Can you ORDER BY 2 things in SQL?

After the ORDER BY keyword, add the name of the column by which you'd like to sort records first (in our example, salary). Then, after a comma, add the second column (in our example, last_name ). You can modify the sorting order (ascending or descending) separately for each column.

Can we have 2 ORDER BY in MySQL?

This sorts your MySQL table result in Ascending or Descending order according to the specified column. The default sorting order is Ascending which you can change using ASC or DESC . SELECT * FROM [table-name] ORDER BY [column-name1 ] [ASC|DESC] , [column-name2] [ASC|DESC],..

How insert multiple orders MySQL?

If you want to sort multiple columns specify all the columns by a comma. column1 DESC, column2 ASC; Here, the result is sorted by column1 in descending order first then, result is sorted by column2 in ascending order.

Can you arrange the result-set of an SQL query on multiple columns?

If you specify multiple columns, the result set is sorted by the first column and then that sorted result set is sorted by the second column, and so on. The columns that appear in the ORDER BY clause must correspond to either column in the select list or columns defined in the table specified in the FROM clause.


1 Answers

SELECT some_cols FROM prefix_users WHERE (some conditions) ORDER BY pic_set DESC, last_activity; 
like image 148
ihorko Avatar answered Oct 20 '22 00:10

ihorko