MySQL根据查询结果更新

时间:2019-10-08 19:14:10

标签: mysql

我希望这不被视为垃圾邮件,今天我问了一个问题:

MySQL overlaping time periods based on criteria

现在我正在尝试基于此查询进行更新查询:

SELECT a.id, GROUP_CONCAT(b.id) AS abcd
FROM new_table a
LEFT JOIN new_table b ON a.id <> b.id
                      AND a.Block_ID = b.Block_ID
                      AND a.Block_Type = b.Block_Type
                      AND a.Valid = b.Valid
                      AND ((a.Valid_To BETWEEN b.Valid_From AND b.Valid_To)
                            OR (a.Valid_From BETWEEN b.Valid_From AND b.Valid_To)
                            OR (a.Valid_To = b.Valid_From)
                            OR (a.Valid_From = b.Valid_To)
                          )
GROUP BY a.id;

我正在尝试更新此表:

ID  |  Valid_From  |  Valid_To  |  Block_Type  |  Valid  |  Block_ID
1      5              10           a              TRUE      1
2      10             15           a              TRUE      1
3      6              10           a              TRUE      1
4      3              4            a              FALSE     1
5      2              4            a              FALSE     1
6      1              1            a              FALSE     1
7      1              10           b              TRUE      1
8      1              10           c              TRUE      2
9      1              10           c              TRUE      2
10     1              15           b              TRUE      1
11     5              10           a              TRUE      1

对于我的数据集,此查询将对此进行重述:

ID  |  abcd
1      2
2      1,11,3
3      2
4      5
5      4
6      null
7      10
8      9
9      8
10     7
11     2

我使用

在表中插入了新列
ALTER TABLE new_table
ADD COLUMN `Overlaping` VARCHAR(45) NULL AFTER `Block_ID`;

现在,我正在尝试使用此查询填充此列,其中我想将abcd列不为空的记录的值更新为“重叠”:

UPDATE new_table nt2,
(SELECT a.id, GROUP_CONCAT(b.id) AS abcd
FROM new_table a
LEFT JOIN new_table b ON a.id <> b.id
                      AND a.Block_ID = b.Block_ID
                      AND a.Block_Type = b.Block_Type
                      AND a.Valid = b.Valid
                      AND ((a.Valid_To BETWEEN b.Valid_From AND b.Valid_To)
                            OR (a.Valid_From BETWEEN b.Valid_From AND b.Valid_To)
                            OR (a.Valid_To = b.Valid_From)
                            OR (a.Valid_From = b.Valid_To)
                          )
GROUP BY a.id) as ac
SET nt2.Overlaping = 'overlaping'
where ac.abcd is not null;

无论abcd是否为空,此查询都会更新每一行,哪里出错了?

非常感谢您

1 个答案:

答案 0 :(得分:0)

内部连接原始表和新的Select

UPDATE new_table nt2 Left join
  (SELECT a.id, GROUP_CONCAT(b.id) AS abcd
   FROM new_table a
     inner JOIN new_table b ON a.id <> b.id
                  AND a.Block_ID = b.Block_ID
                  AND a.Block_Type = b.Block_Type
                  AND a.Valid = b.Valid
                  AND ((a.Valid_To BETWEEN b.Valid_From AND b.Valid_To)
                        OR (a.Valid_From BETWEEN b.Valid_From AND b.Valid_To)
                        OR (a.Valid_To = b.Valid_From)
                        OR (a.Valid_From = b.Valid_To)
                      )
                GROUP BY a.id) as ac 
   on ac.id = nt.id
SET nt2.Overlaping = ac.abcd
where ac.abcd is not null;