简体   繁体   English

sql - 在子查询和同一个表中更新

[英]sql - update in subquery & same table

I want to update multiple lines of data, but an error occurred.我想更新多行数据,但发生错误。

Below is the table information and query statement.下面是表信息和查询语句。

myTable我的表

ltiNum  ltiCd   updateDate  ClosedOrNot

1   A   2020-07-01  
2   B   2020-07-01  
3   C   2020-07-01  
4   D   2020-07-01  
5   E   2020-07-01  
1   A   2020-08-01  
3   C   2020-08-01  
4   D   2020-08-01  
5   E   2020-08-01  
6   F   2020-08-01  

SQL SQL

update myTable 
set closedOrNot = 
        (case when (SELECT CONCAT(ltiNum,ltiCd) FROM myTable where updateDate Like '2020-08%'
                    and CONCAT(ltiNum,ltiCd) in (SELECT CONCAT(ltiNum,ltiCd) FROM myTable WHERE updateDate Like '2020-07%'       
        )) then 'existing'
        when (SELECT CONCAT(ltiNum,ltiCd) FROM myTable where updateDate Like '2020-08%'
                    and CONCAT(ltiNum,ltiCd) not in (SELECT CONCAT(ltiNum,ltiCd) FROM myTable WHERE updateDate Like '2020-07%'
        )) then 'New'        
         when (SELECT CONCAT(ltiNum,ltiCd) FROM myTable where updateDate Like '2020-07%'
                    and CONCAT(ltiNum,ltiCd) not in (SELECT CONCAT(ltiNum,ltiCd) FROM myTable WHERE updateDate Like '2020-08%'
        )) then 'Closed'
    end);

Error code :错误代码 :

mysql error code 1093: You can't specify target table for 'myTable' update in FROM clause mysql 错误代码 1093:您无法在 FROM 子句中为“myTable”更新指定目标表

Where is the problem?问题出在哪儿?

i want see follow images我想看后续图片

please help~!请帮忙~!

在此处输入图片说明

You must encapsule the mytable with in a SELECT您必须用 SELECT 封装 mytable

update myTable 
set closedOrNot = 
        (case when (SELECT CONCAT(ltiNum,ltiCd) FROM (SELECT * FROM myTable) t1 where updateDate Like '2020-08%'
                    and CONCAT(ltiNum,ltiCd) in (SELECT CONCAT(ltiNum,ltiCd) FROM (SELECT * FROM myTable) t1 WHERE updateDate Like '2020-07%'       
        )) then 'existing'
        when (SELECT CONCAT(ltiNum,ltiCd) FROM (SELECT * FROM myTable) t1 where updateDate Like '2020-08%'
                    and CONCAT(ltiNum,ltiCd) not in (SELECT CONCAT(ltiNum,ltiCd) FROM (SELECT * FROM myTable) t1 WHERE updateDate Like '2020-07%'
        )) then 'New'        
         when (SELECT CONCAT(ltiNum,ltiCd) FROM (SELECT * FROM myTable) t1 where updateDate Like '2020-07%'
                    and CONCAT(ltiNum,ltiCd) not in (SELECT CONCAT(ltiNum,ltiCd) FROM (SELECT * FROM myTable) t1 WHERE updateDate Like '2020-08%'
        )) then 'Closed'
    end);

So mysql thinks, that this is another table, but you are basically changing all the rows, because you didn't use a WHERE clause, so that it could happen that further the road, the results could be changed所以mysql认为,这是另一个表,但你基本上改变了所有的行,因为你没有使用WHERE子句,所以可能会发生在更远的路上,结果可能会改变

Basically everything is correct what o told you but your query has another problem基本上一切都是正确的 o 告诉你的但你的查询有另一个问题

CREATE TABLE myTable ( `ltiNum` INTEGER, `ltiCd` VARCHAR(1), `updateDate` VARCHAR(10), `closedOrNot` VARCHAR(8) ); INSERT INTO myTable (`ltiNum`, `ltiCd`, `updateDate`, `closedOrNot`) VALUES ('1', 'A', '2020-07-01', 'NULL'), ('2', 'B', '2020-07-01', 'Closed'), ('3', '0', '2020-07-01', 'NULL'), ('4', 'D', '2020-07-01', 'NULL'), ('5', 'E', '2020-07-01', 'NULL'), ('1', 'A', '2020-08-01', 'existing'), ('3', '0', '2020-08-01', 'existing'), ('4', 'D', '2020-08-01', 'existing'), ('5', 'E', '2020-08-01', 'existing'), ('6', 'F', '2020-08-01', 'New');
 update myTable set closedOrNot = (case when (SELECT CONCAT(ltiNum,ltiCd) FROM (SELECT * FROM myTable) t1 where updateDate Like '2020-08%' and CONCAT(ltiNum,ltiCd) in (SELECT CONCAT(ltiNum,ltiCd) FROM (SELECT * FROM myTable) t1 WHERE updateDate Like '2020-07%' )) then 'existing' when (SELECT CONCAT(ltiNum,ltiCd) FROM (SELECT * FROM myTable) t1 where updateDate Like '2020-08%' and CONCAT(ltiNum,ltiCd) not in (SELECT CONCAT(ltiNum,ltiCd) FROM (SELECT * FROM myTable) t1 WHERE updateDate Like '2020-07%' )) then 'New' when (SELECT CONCAT(ltiNum,ltiCd) FROM (SELECT * FROM myTable) t1 where updateDate Like '2020-07%' and CONCAT(ltiNum,ltiCd) not in (SELECT CONCAT(ltiNum,ltiCd) FROM (SELECT * FROM myTable) t1 WHERE updateDate Like '2020-08%' )) then 'Closed' end);
\nSubquery returns more than 1 row子查询返回超过 1 行\n
 SELECT CONCAT(ltiNum,ltiCd) FROM (SELECT * FROM myTable) t1 where updateDate Like '2020-08%' and CONCAT(ltiNum,ltiCd) in (SELECT CONCAT(ltiNum,ltiCd) FROM (SELECT * FROM myTable) t1 WHERE updateDate Like '2020-07%' )
\n| | CONCAT(ltiNum,ltiCd) | CONCAT(ltiNum,ltiCd) |\n| | :------------------- | :-------------------- |\n| | 1A | 1A |\n| | 30 | 30 |\n| | 4D | 4D |\n| | 5E | 5E |\n
SELECT CONCAT(ltiNum,ltiCd) FROM (SELECT * FROM myTable) t1 where updateDate Like '2020-08%' and CONCAT(ltiNum,ltiCd) not in (SELECT CONCAT(ltiNum,ltiCd) FROM (SELECT * FROM myTable) t1 WHERE updateDate Like '2020-07%' )
\n| | CONCAT(ltiNum,ltiCd) | CONCAT(ltiNum,ltiCd) |\n| | :------------------- | :-------------------- |\n| | 6F | 6F |\n
SELECT CONCAT(ltiNum,ltiCd) FROM (SELECT * FROM myTable) t1 where updateDate Like '2020-07%' and CONCAT(ltiNum,ltiCd) not in (SELECT CONCAT(ltiNum,ltiCd) FROM (SELECT * FROM myTable) t1 WHERE updateDate Like '2020-08%' )
\n| | CONCAT(ltiNum,ltiCd) | CONCAT(ltiNum,ltiCd) |\n| | :------------------- | :-------------------- |\n| | 2B | 2B |\n

db<>fiddle here db<> 在这里摆弄

As you can see the first CASE retursn 4 results, so that your construction doesn't work-如您所见,第一个 CASE 返回 4 个结果,因此您的构建不起作用-

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

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