如何在MySQL中进行不同的排序?
我有这个: 从TA中选择put\U id、ut\U id、cm\U tx、da、un如何在MySQL中进行不同的排序?,mysql,sql,mysql-workbench,Mysql,Sql,Mysql Workbench,我有这个: 从TA中选择put\U id、ut\U id、cm\U tx、da、un ut_id | put_id | cm_tx | da | un ------|---------|--------------------------|------------|-------- 21 | 21 | was good | 1523190974 | Jonah 22 | 2
ut_id | put_id | cm_tx | da | un
------|---------|--------------------------|------------|--------
21 | 21 | was good | 1523190974 | Jonah
22 | 21 | thx | 1523197793 | Sara
23 | 23 | that was good post | 1523201196 | Tom
24 | 24 | not good | 1523208390 | Lucas
25 | 24 | not good?? | 1523718726 | Stephen
26 | 24 | why u said not good? | 1523718805 | Stephen
27 | 24 | tell me why u said? | 1523718886 | Stephen
我想要的是:
ut_id | put_id | cm_tx | da | un
------|---------|--------------------------|------------|--------
21 | 21 | was good | 1523190974 | Jonah
22 | 21 | thx | 1523197793 | Sara
23 | 23 | that was good post | 1523201196 | Tom
24 | 24 | not good | 1523208390 | Lucas
27 | 24 | tell me why u said? | 1523718886 | Stephen
26 | 24 | why u said not good? | 1523718805 | Stephen
25 | 24 | not good?? | 1523718726 | Stephen
(事实上,我想按da和put_id对唯一的“ut_id!=put_id”)订单进行排序
提前感谢我从您想要的输出中了解到,您希望按put\u id排序,然后按ut\u id=put\u id,然后按日期降序排序。您可以通过以下方式实现这一点:
SELECT put_id,ut_id,cm_tx,date,un FROM TA ORDER BY put_id ASC, IF(ut_id = put_id, 0, 1) ASC, date DESC
输出:
put_id ut_id cm_tx date un
21 21 was good 1523190974 Jonah
21 22 thx 1523197793 Sara
23 23 that was good post 1523201196 Tom
24 24 not good 1523208390 Lucas
24 27 tell me why u said? 1523718886 Stephen
24 26 why u said not good? 1523718805 Stephen
24 25 not good?? 1523718726 Stephen
您是否尝试过按put_id asc、put_id descyes下单,但这不起作用这是此订单的逻辑依据???我想要排序ut_id!=顺序背后的逻辑不清楚,至少对我来说不清楚<代码>按ut_id订购!=put_id没有任何意义。谢谢,你准确地解决了问题