Skip to content
Advertisement

Target table cannot be same one for update clause in mysql

I have a table name named ’employee’. Table creation code given below:

The table content is like below:

Now I want to update ‘e_id’, first it will check whether the same e_id is in the table anywhere or not, if it is not in the table then only it will update the rows with given e_id, else it will not going to update. So, my upgradation query is below:

But it is giving the following error:

ERROR 1093 (HY000): You can’t specify target table ’employee’ for update in FROM clause I have tried the below query:

But this also cannot update the table and showing below result:

also tried the below code:

but this also cannot update the table and gives below result: Query OK, 0 rows affected (0.00 sec) Rows matched: 0 Changed: 0 Warnings: 0 Please help. Thank you in advance.

Advertisement

Answer

NULL doesn’t play the way some people expect with NOT IN: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=24c176ff4d4e2c52309aaca14cc121c5 So, just put WHERE e_id IS NOT NULL in the sub-query. Also, HAVING COUNT(*) >= 1 can be removed as it’s always going to return a value of 1 or more…

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=2a0b036a7d1db9138e3ab29af3d346f8

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement