简体   繁体   English

如何将mysql中最后一行的位置更改为第一行?

[英]How to change position of last row in mysql to first row?

my sql query is我的 sql 查询是

SELECT COUNT(*) as count FROM <DB> GROUP BY InputTime ORDER BY InputTime) A GROUP BY HOUR(InputTime) WITH ROLLUP;

And the result is结果是

+-------+
| count |
+-------+
|    62 |
|   200 |
|    10 |
|   272 |
+-------+

Last row is the sum of previous values.最后一行是先前值的总和。 How to make last row to first row like this?如何像这样将最后一行变成第一行?

+-------+
| count |
+-------+
|   272 |
|    62 |
|   200 |
|    10 |
+-------+

I'd select the hour(inputtime) too, and order by it.我也会选择小时(输入时间),并按它订购。 The roll up line will have a null for it and nulls sort first.汇总行将有一个空值,空值首先排序。

Perhaps something like this:也许是这样的:

SELECT IFNULL(hours,'Total'), count
FROM
(SELECT HOUR(InputTime) hours, COUNT(*) AS count 
FROM DBname where InputTime>= '2021-07-07' AND InputTime <='2021-07-07 23:56:59' GROUP BY HOUR(InputTime) WITH ROLLUP) V
ORDER BY hours ASC;

Since the ROLLUP returned NULL for hours column in the subquery, the ORDER BY hours in the outer query will put it on top.由于ROLLUP在子查询中的hours列返回NULL ,因此外部查询中的ORDER BY hours将把它放在最前面。 " .. In MySQL NULL values are considered lower than any non-NULL value, therefore, NULL values appear first when the order is ASC (ascending), and ordered last when the order is DESC (descending) .. " ( https://www.designcise.com/web/tutorial/how-to-order-null-values-first-or-last-in-mysql ) .. 在 MySQL 中,NULL 值被认为低于任何非 NULL 值,因此,当顺序为 ASC(升序)时,NULL 值首先出现,当顺序为 DESC(降序)时排在最后......https:/ /www.designcise.com/web/tutorial/how-to-order-null-values-first-or-last-in-mysql )

Demo fiddle演示小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM