【问题标题】:mysql not ordering using Order by using DESCmysql 不使用 Order by using DESC 进行排序
【发布时间】:2017-06-08 17:06:10
【问题描述】:

在ubuntu上使用mysql

以下命令未按降序排序

mysql> select spo_id, count(spo_id) as "maxCount" from order_details GROUP BY spo_id ORDER BY "maxCount" DESC;
+--------+----------+
| spo_id | maxCount |
+--------+----------+
|      1 |        1 |
|      2 |        3 |
|      3 |        1 |
+--------+----------+
3 rows in set (0.00 sec)

【问题讨论】:

  • 您正在按常量/字符串“maxCount”进行排序,这类似于说ORDER BY 1 DESC,这是毫无意义的。
  • @JNevill: ORDER BY 1 DESC 对于大多数 DBMS 来说是可以接受的(不确定 MySQL)。这意味着按 SELECT 列表中的第一列排序。在这种特定情况下,ORDER BY 2 DESC 将完成发布者的意图。
  • @KenWhite 哦,天哪。是的,我的错。这是一个不好的例子。 ORDER BY "somestring" desc 会更好。

标签: mysql sql select sql-order-by


【解决方案1】:

MySQL 允许使用双引号的字符串文字。因此,当您按“maxCount”排序时,实际上是按字符串文字排序,这毫无意义。删除引号,它应该可以正常工作:

MariaDB [db]> select spo_id, count(spo_id) as maxCount from order_details GROUP BY spo_id ORDER BY maxCount DESC;
+--------+----------+
| spo_id | maxCount |
+--------+----------+
|      2 |        3 |
|      3 |        1 |
|      1 |        1 |
+--------+----------+
3 rows in set (0.00 sec)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-18
    • 2014-02-07
    • 1970-01-01
    • 2015-02-26
    • 1970-01-01
    • 2011-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多