Good afternoon!

I have this SQL query:

SELECT u.id, u.avatar, u.city, u.f_name, u.s_name, u.notes, u.username, SUM(b.summ) AS summ FROM users AS u LEFT JOIN balance AS b ON b.user_id = u.id WHERE $conditions GROUP BY u.id ORDER BY summ DESC; 

As you can see, there is a sorting by the summ field, but I also need to sort by the fields, for example, two users:

  1. Ivan Savkov NULL NULL 5000
  2. Kostya Malikov Moscow 1 5000

As you can see, both are 5,000 each, but Ivan is the first on the list, and I need the second one, because not all of his fields are filled out.

Here is such a task, thanks)

    2 answers 2

    It was necessary to do this a little earlier, of course, you can try sorting on the 3rd column (where you have zero and 1, and then sorting on the 2nd column, SORT - I think you know how to use). And it is even easier during the filling to make another field of "priority", the more filled, the higher the priority, and sort by it, and everything will be perfect.

    • The idea is good, maybe when I need a more accurate sorting, I will. - chuikoff

    And what prevents to add other columns to the sorting, where it can be NULL, for example:

     ORDER BY summ DESC, u.notes desc;