如何在MySQL中实现多样化排序?附查询示例与需求
Let's walk through various sorting approaches you can apply to your query, starting with your base query and original output:
Base Query & Original Results
Your initial query:
SELECT put_id, ut_id, cm_tx, da, un FROM TA;
Original output:
| 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 |
1. Sort by ut_id (Ascending/Descending)
To order results by the unique comment ID (ut_id), add an ORDER BY clause. Ascending is the default, but you can explicitly specify it, or use DESC for descending:
Ascending (matches original order):
SELECT put_id, ut_id, cm_tx, da, un FROM TA ORDER BY ut_id ASC;
Result is identical to your original output.
Descending:
SELECT put_id, ut_id, cm_tx, da, un FROM TA ORDER BY ut_id DESC;
Result:
| ut_id | put_id | cm_tx | da | un |
|---|---|---|---|---|
| 27 | 24 | tell me why u said? | 1523718886 | Stephen |
| 26 | 24 | why u said not good? | 1523718805 | Stephen |
| 25 | 24 | not good?? | 1523718726 | Stephen |
| 24 | 24 | not good | 1523208390 | Lucas |
| 23 | 23 | that was good post | 1523201196 | Tom |
| 22 | 21 | thx | 1523197793 | Sara |
| 21 | 21 | was good | 1523190974 | Jonah |
2. Group by Post (put_id) & Sort by Comment Time (da)
If you want to cluster all comments under the same post and order them by when they were made (using the Unix timestamp da), use this query:
SELECT put_id, ut_id, cm_tx, da, un FROM TA ORDER BY put_id ASC, da ASC;
Result (comments per post ordered oldest to newest):
| 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 |
To reverse the comment order (newest first per post), swap da ASC for da DESC:
SELECT put_id, ut_id, cm_tx, da, un FROM TA ORDER BY put_id ASC, da DESC;
Result:
| ut_id | put_id | cm_tx | da | un |
|---|---|---|---|---|
| 22 | 21 | thx | 1523197793 | Sara |
| 21 | 21 | was good | 1523190974 | Jonah |
| 23 | 23 | that was good post | 1523201196 | Tom |
| 27 | 24 | tell me why u said? | 1523718886 | Stephen |
| 26 | 24 | why u said not good? | 1523718805 | Stephen |
| 25 | 24 | not good?? | 1523718726 | Stephen |
| 24 | 24 | not good | 1523208390 | Lucas |
3. Group by User (un) & Sort by Comment Time
To see all comments from a single user grouped together, ordered by when they posted:
SELECT put_id, ut_id, cm_tx, da, un FROM TA ORDER BY un ASC, da ASC;
Result:
| ut_id | put_id | cm_tx | da | un |
|---|---|---|---|---|
| 21 | 21 | was good | 1523190974 | Jonah |
| 24 | 24 | not good | 1523208390 | Lucas |
| 22 | 21 | thx | 1523197793 | Sara |
| 25 | 24 | not good?? | 1523718726 | Stephen |
| 26 | 24 | why u said not good? | 1523718805 | Stephen |
| 27 | 24 | tell me why u said? | 1523718886 | Stephen |
| 23 | 23 | that was good post | 1523201196 | Tom |
4. Sort by Comment Length (cm_tx character count)
If you want to order comments by how long they are, use LENGTH(cm_tx) in the ORDER BY clause:
SELECT put_id, ut_id, cm_tx, da, un FROM TA ORDER BY LENGTH(cm_tx) ASC;
Result (shortest comments first):
| ut_id | put_id | cm_tx | da | un |
|---|---|---|---|---|
| 22 | 21 | thx | 1523197793 | Sara |
| 21 | 21 | was good | 1523190974 | Jonah |
| 24 | 24 | not good | 1523208390 | Lucas |
| 25 | 24 | not good?? | 1523718726 | Stephen |
| 23 | 23 | that was good post | 1523201196 | Tom |
| 26 | 24 | why u said not good? | 1523718805 | Stephen |
| 27 | 24 | tell me why u said? | 1523718886 | Stephen |
For longest comments first, use DESC:
SELECT put_id, ut_id, cm_tx, da, un FROM TA ORDER BY LENGTH(cm_tx) DESC;
内容的提问来源于stack exchange,提问作者Hamed M

