You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在MySQL中实现多样化排序?附查询示例与需求

Custom Sorting for Your SQL Query Results

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_idput_idcm_txdaun
2121was good1523190974Jonah
2221thx1523197793Sara
2323that was good post1523201196Tom
2424not good1523208390Lucas
2524not good??1523718726Stephen
2624why u said not good?1523718805Stephen
2724tell me why u said?1523718886Stephen

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_idput_idcm_txdaun
2724tell me why u said?1523718886Stephen
2624why u said not good?1523718805Stephen
2524not good??1523718726Stephen
2424not good1523208390Lucas
2323that was good post1523201196Tom
2221thx1523197793Sara
2121was good1523190974Jonah

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_idput_idcm_txdaun
2121was good1523190974Jonah
2221thx1523197793Sara
2323that was good post1523201196Tom
2424not good1523208390Lucas
2524not good??1523718726Stephen
2624why u said not good?1523718805Stephen
2724tell me why u said?1523718886Stephen

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_idput_idcm_txdaun
2221thx1523197793Sara
2121was good1523190974Jonah
2323that was good post1523201196Tom
2724tell me why u said?1523718886Stephen
2624why u said not good?1523718805Stephen
2524not good??1523718726Stephen
2424not good1523208390Lucas

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_idput_idcm_txdaun
2121was good1523190974Jonah
2424not good1523208390Lucas
2221thx1523197793Sara
2524not good??1523718726Stephen
2624why u said not good?1523718805Stephen
2724tell me why u said?1523718886Stephen
2323that was good post1523201196Tom

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_idput_idcm_txdaun
2221thx1523197793Sara
2121was good1523190974Jonah
2424not good1523208390Lucas
2524not good??1523718726Stephen
2323that was good post1523201196Tom
2624why u said not good?1523718805Stephen
2724tell me why u said?1523718886Stephen

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:24:05