0

I'm trying to get answers with ordering by text and timestamp, firs must be answers with text = null, next with text not null ordered by ts, but it doesn't work:

SELECT * FROM answers WHERE user_id = 1279942 ORDER BY text, ts desc;
+----------+---------+-------------+------------+-------------------------------+--------+
| id       | user_id | question_id | ts         | text                          | is_new |
+----------+---------+-------------+------------+-------------------------------+--------+
| 81187563 | 1279942 |    30918210 | 1487186969 | NULL                          |      0 |
| 81187560 | 1279942 |    30918209 | 1487117018 | NULL                          |      0 |
| 81187559 | 1279942 |    30918208 | 1487116399 | NULL                          |      0 |
| 81187557 | 1279942 |    30918205 | 1487024673 | Jg jg jg                      |      1 |
| 81187555 | 1279942 |    30918205 | 1487022656 | Jg jg jg                      |      1 |
|        7 | 1279942 |    30915697 | 1397915529 | lf                            |      0 |
| 81187535 | 1279942 |    30915687 | 1397047472 | да                            |      0 |
|        8 | 1279942 |    30915697 | 1397990298 | да все ок                     |      0 |
| 81187543 | 1279942 |    30915688 | 1397047582 | Да ничего                     |      0 |
| 81187561 | 1279942 |    30918206 | 1487186497 | Леха, все супер!              |      1 |
| 81187556 | 1279942 |    30918205 | 1487022667 | Оп оп                         |      1 |
| 81187558 | 1279942 |    30918207 | 1487186977 | Последний ответ               |      1 |
| 81187562 | 1279942 |    30917194 | 1487186733 | ТЕСТОВЫЙ ОТВЕТ                |      1 |
|     1537 | 1279942 |    30917195 | 1413380315 | фыв                           |      0 |
| 81187547 | 1279942 |    30915691 | 1397048820 | щл                            |      0 |
+----------+---------+-------------+------------+-------------------------------+--------+
15 rows in set (0.00 sec)

on the 4th row must be answer with id 81187558. Who know what can cause it? 😩

0

1 Answer 1

2

Untested, but you could probably use something like this to order in the fashion you're after:

SELECT * FROM answers WHERE user_id = 1279942 
    ORDER BY ISNULL(text) DESC, ts DESC;

Note that ISNULL() returns 0 if the value is not null, and 1 if it is. Thus by sorting that in a descending order first, we place all of the records where text is NULL at the top. This creates two buckets that all share a ISNULL value. So the remainder will be sorted only by ts since ISNULL are all 0.

Your original approach sorts by text first, and then for each bucket of shared text values, it sorts those internally by ts.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow.. It works, but what is the difference between using isnull and just ordering by column?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.