mysql 不使用 Order by using DESC 进行排序

标签 mysql sql select sql-order-by

在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)

最佳答案

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)

关于mysql 不使用 Order by using DESC 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44441778/

相关文章:

mysql - 每个唯一 ID 的计数值

php - 为什么在使用 MySQLi 在 PHP 中准备句子时显示错误

java - 如何使用eclipse连接java与MySQL数据库

mysql - (STOREDPROC) 生成的 SELECT 语句 : looking for the right syntax to use near NULL

java - 用于确定高尔夫应用程序中发球区顺序的 SQL

javascript - 在选择标签而不是文本中显示值

javascript - 如何通过 refs 以编程方式在 React 中设置选定的值?

php - 如何检查数据库中已存在的值?

mysql - 我应该将用户上传的照片存储在哪里?

mysql - 在 MySQL 中具有长度(字段)