MySql order by date asc, followed by user_id -


what query should make achieved outcome

here rows

 user_id    | date 3          | 2017-09-07 2          | 2017-09-08 3          | 2017-09-10 

and desired output in query this

 user_id    | date 3          | 2017-09-07 3          | 2017-09-10 2          | 2017-09-08 

but it's not based on user_id, want query rows order date asc , user_id next each other.

you can use following query:

select user_id, min(date) min_date mytable group user_id 

to earliest-per-user date value.

using above query derived table can achieve required ordering:

select t1.user_id, t1.date mytable t1 join (    select user_id, min(date) min_date    mytable    group user_id ) t2 on t1.user_id = t2.user_id order t2.min_date, user_id, t1.date 

note: in case 2 or more users share same earliest date query gives precedence user having smallest user_id value.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -