简体   繁体   中英

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

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

Where is the problem?

i want see follow images

please help~!

在此处输入图片说明

You must encapsule the mytable with in a SELECT

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

Basically everything is correct what o told you but your query has another problem

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\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) | \n|  :------------------- | \n|  1A | \n|  30 | \n|  4D | \n|  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) | \n|  :------------------- | \n|  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) | \n|  :------------------- | \n|  2B | \n

db<>fiddle here

As you can see the first CASE retursn 4 results, so that your construction doesn't work-

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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