如何在mysql中创建一个逆序变量

Posted

技术标签:

【中文标题】如何在mysql中创建一个逆序变量【英文标题】:How to create an inverse ordered variable in my sql 【发布时间】:2020-05-28 18:13:26 【问题描述】:

我有一列运行总和,但我想在新列或同一列中颠倒顺序

我的查询是

SELECT date,
ant, 
num_room,
(@csum:= @csum + num_room) as cumulative_room
from xxx_xxxx_xxxx
WHERE date BETWEEN CURDATE() - INTERVAL 30 DAY AND CURDATE()
AND(select @csum := 0) = 0
order by date

米表是

 date         | ant  | num_room  | cumulative_room
    28-04-2020       6       1              1  
    28-04-2020       3       1              2
    28-04-2020       4       1              3
    28-04-2020       7       1              4
    28-04-2020       4       1              5
    ....

我怎样才能从累积空间做相反顺序的另一个变量?

 date         | ant  | num_room  | cumulative_room |reverse_cumulative room
28-04-2020       6       1              1                5
28-04-2020       3       1              2                4
28-04-2020       4       1              3                3
28-04-2020       7       1              4                2
28-04-2020       4       1              5                1
    ....

【问题讨论】:

mysql 8.x 中很容易。您使用的是 MySQL 5.x 还是 8.x? 我使用的是 MYSQL 5.6.4 【参考方案1】:

你的完整代码是

SELECT
t1.*
, t2.max_room - `cumulative_room` 'reverse_cumulative room'
FROm (SELECT date,
                ant, 
                num_room,
                (@csum:= @csum + num_room) as cumulative_room
                from xxx_xxxx_xxxx
                WHERE date BETWEEN CURDATE() - INTERVAL 30 DAY AND CURDATE()
                AND(select @csum := 0) = 0
                order by date) t1
INNER JOIN (SELECT MAX(`cumulative_room`) +1 max_room,  `date` FROM (SELECT date,
                                                                            ant, 
                                                                            num_room,
                                                                            (@csum:= @csum + num_room) as cumulative_room
                                                                            from xxx_xxxx_xxxx
                                                                            WHERE date BETWEEN CURDATE() - INTERVAL 30 DAY AND CURDATE()
                                                                            AND(select @csum := 0) = 0
                                                                            order by date) t3 GROUP BY  `date`) t2
ON t1.`date` = t2.`date`;

这背后的想法是

CREATE TABLE xxx_xxxx_xxxx (
  `date` VARCHAR(10),
  `ant` INTEGER,
  `num_room` INTEGER,
  `cumulative_room` INTEGER
);

INSERT INTO xxx_xxxx_xxxx
  (`date`, `ant`, `num_room`, `cumulative_room`)
VALUES
  ('28-04-2020', '6', '1', '1'),
  ('28-04-2020', '3', '1', '2'),
  ('28-04-2020', '4', '1', '3'),
  ('28-04-2020', '7', '1', '4'),
  ('28-04-2020', '4', '1', '5');
✓ ✓
SELECT
t1.*
,t2.max_room - `cumulative_room` 'reverse_cumulative room'
FROm xxx_xxxx_xxxx t1
INNER JOIN (SELECT MAX(`cumulative_room`) +1 max_room,  `date` FROM xxx_xxxx_xxxx GROUP BY  `date`) t2
ON t1.`date` = t2.`date`;
日期 |蚂蚁|房间数 |累积房间 |反向累积房间 :--------- | --: | --------: | --------------: | ----------------------: 28-04-2020 | 6 | 1 | 1 | 5 28-04-2020 | 3 | 1 | 2 | 4 28-04-2020 | 4 | 1 | 3 | 3 28-04-2020 | 7 | 1 | 4 | 2 28-04-2020 | 4 | 1 | 5 | 1

db小提琴here

【讨论】:

感谢您的回复。我不知道为什么,但是我有一个错误“[mysqld-5.6.40]每个派生表都必须有自己的别名” 再次感谢,现在我尝试通过蚂蚁订购,我想通过蚂蚁获得reverse_cumulative房间。 我不明白您试图对您的问题或我的结果中的代码进行排序? 我不得不用子查询更改我的代码,我也有同样的问题 像我的帖子一样写一个小提琴,我们看看能做些什么

以上是关于如何在mysql中创建一个逆序变量的主要内容,如果未能解决你的问题,请参考以下文章

如何在 DOSBox 的子程序中创建变量?

如何在公共方法中创建私有变量

如何在 mySQL 中创建一个空的 VIEW

如何使用 PHP 在 MySQL 中创建表?

如何在 MySQL 中创建表别名

sql 如何在MySql中创建一个columun