Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I would like to order this data by datetime field in descendent with phone number "grouped by".

datetime           |telephone|user               |
-------------------|---------|-------------------|
2019-11-15 20:41:43|      123|proident, sunt in  |
2019-10-08 09:49:33|      456|a deserunt mollit  |
2019-08-13 12:38:16|   123334|d tempor incididun |
2019-07-24 21:44:15|123456789|sectetur adipisci  |
2019-06-14 15:03:32|123456789|it, sed do eiusmod |
2019-05-20 07:09:01|   234234|aliqua. Ut enim    |
2019-05-10 23:22:32| 90812345|Lorem ipsum dolor  |
2019-03-25 17:27:37|   123334|fficia deserunt mo |
2019-02-25 22:32:19|      123| labore et dolore  |
2019-02-18 16:01:05|123456789|lit, sed do eiusmo |

I have tried this but it is not enough.

SELECT * FROM(select * from test order by test.`datetime` desc LIMIT 
18446744073709551615) as sub;

For example, I would like to have telephone 123 at first place repeated twice times with their datetime field in descendent order.

datetime           |telephone|user               |
-------------------|---------|-------------------|
2019-11-15 20:41:43|      123|proident, sunt in  |
2019-02-25 22:32:19|      123| labore et dolore  |
2019-10-08 09:49:33|      456|a deserunt mollit  |
2019-08-13 12:38:16|   123334|d tempor incididun |
2019-07-24 21:44:15|123456789|sectetur adipisci  |
2019-06-14 15:03:32|123456789|it, sed do eiusmod |
2019-02-18 16:01:05|123456789|lit, sed do eiusmo |
2019-05-20 07:09:01|   234234|aliqua. Ut enim    |
2019-05-10 23:22:32| 90812345|Lorem ipsum dolor  |
2019-03-25 17:27:37|   123334|fficia deserunt mo |
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
120 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can achieve this result by self-joining your table to a table of the max values of datetime for each value of telephone, and then sorting based on those max values, followed by the actual datetime value for that row:

SELECT t1.* 
FROM test t1
JOIN (SELECT telephone, MAX(datetime) AS datetime
      FROM test
      GROUP BY telephone) t2 ON t2.telephone = t1.telephone
ORDER BY t2.datetime DESC, t1.datetime DESC

Output:

datetime            telephone   user
2019-11-15 20:41:43 123         proident, sunt in
2019-02-25 22:32:19 123         labore et dolore
2019-10-08 09:49:33 456         a deserunt mollit
2019-08-13 12:38:16 123334      d tempor incididun
2019-03-25 17:27:37 123334      fficia deserunt mo
2019-07-24 21:44:15 123456789   sectetur adipisci
2019-06-14 15:03:32 123456789   it, sed do eiusmod
2019-02-18 16:01:05 123456789   lit, sed do eiusmo
2019-05-20 07:09:01 234234      aliqua. Ut enim
2019-05-10 23:22:32 90812345    Lorem ipsum dolor

Demo on dbfiddle


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...