Home > Blockchain >  MySql Query Building help needed
MySql Query Building help needed

Time:11-21

users table

id name company_id
1 user 1 1
2 user 2 2
3 user 3 1

How can I select all the company_id uniquely while ordering them by user name ?

I have tried below query in ONLY_FULL_GROUP_BY false mode

select
  distinct `company_id`
from
  `users`
order by
  `name` DESC

But this messed up my ordering.

CodePudding user response:

So based on your requirements you'd need to do something like:

select `company_id`
from
  `users`
group by `company_id`
order by MAX(`name`) DESC

This should group company ids and order them based on the descending order of the last name that appears in that group.

  • Related