如何在 MySQL 中执行 2 Order By 子句?

Posted

技术标签:

【中文标题】如何在 MySQL 中执行 2 Order By 子句?【英文标题】:How can I execute 2 Order By clause in MySQL? 【发布时间】:2021-10-11 01:03:26 【问题描述】:

我目前在这里遇到了这个问题。我可以知道如何在查询中包含 2 个 ORDER BY 子句,以便我可以通过 DESC 订购 Main_Actor_Payment 和 Supporting_Actor_Payment。有了我现在的条款,我只能订购supporting_actor_payment。有人可以帮我按降序订购它们。非常感谢。

Results of current query

SELECT main_actor.First_Name,
        main_actor.Last_Name,
        main_actor.Payment_amount AS MAIN_ACTOR_PAYMENT,
        supporting_actors.First_Name,
        supporting_actors.Last_Name,
        supporting_actors.Payment_amount AS SUPPORTING_ACTOR_PAYMENT
FROM main_actor
INNER JOIN supporting_actors USING(MOVIE_ID)
ORDER BY supporting_actors.Payment_amount DESC

【问题讨论】:

提供样本数据作为 CREATE TABLE + INSERT INTO(3-5 行)和该数据的所需输出。作为格式化代码和格式化表格,不是截图! 【参考方案1】:

尝试在ORDER BY 子句中的supporting_actors.Payment_amount 旁边添加main_actor.Payment_amount

SELECT main_actor.First_Name,
        main_actor.Last_Name,
        main_actor.Payment_amount AS MAIN_ACTOR_PAYMENT,
        supporting_actors.First_Name,
        supporting_actors.Last_Name,
        supporting_actors.Payment_amount AS SUPPORTING_ACTOR_PAYMENT
FROM main_actor
INNER JOIN supporting_actors USING(MOVIE_ID)
ORDER BY supporting_actors.Payment_amount, main_actor.Payment_amount DESC

来自

ORDER BY supporting_actors.Payment_amount DESC

ORDER BY supporting_actors.Payment_amount, main_actor.Payment_amount DESC

你是这个意思吗?

【讨论】:

是的,我之前尝试过,但它也不起作用 能否提供一个示例表数据和表结构? 请给我您的电子邮件地址,以便我可以将完整的脚本发送给您,这样会更容易。谢谢。 @ryan 我不认为我们可以在这里发布我们的电子邮件地址 没关系,我会发布我的,naveenjc10@gmail.com。请收件箱给我。

以上是关于如何在 MySQL 中执行 2 Order By 子句?的主要内容,如果未能解决你的问题,请参考以下文章

mysql:group by,order by

java中如何实现mysql中的group by order by count()功能

21《MySQL 教程》ORDER BY 排序

Mysql - ORDER BY详解

MySQL的order by该如何避免“未命中索引“

MySQL的order by该如何避免“未命中索引“