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:

create table employee(name varchar(50),ph_no varchar(10),e_id varchar(5),pay_scale varchar(5),year varchar(4));

The table content is like below:

insert into employee(name,ph_no,pay_scale,year) values('AMIT','123456','PL-10','2019');
insert into employee(name,ph_no,pay_scale,year) values('AMIT','123456','PL-10','2020');
insert into employee(name,ph_no,pay_scale,year) values('AMIT','123456','PL-11','2021');
insert into employee(name,ph_no,pay_scale,year) values('AMIT','123456','PL-11','2022');

+------+--------+------+-----------+------+
| name | ph_no  | e_id | pay_scale | year |
+------+--------+------+-----------+------+
| AMIT | 123456 | NULL | PL-10     | 2019 |
| AMIT | 123456 | NULL | PL-10     | 2020 |
| AMIT | 123456 | NULL | PL-11     | 2021 |
| AMIT | 123456 | NULL | PL-11     | 2022 |
+------+--------+------+-----------+------+

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:

update employee 
set e_id='0132' 
where concat_ws(',',name,ph_no,pay_scale)=concat_ws(',','AMIT','123456','PL-10') 
  and not exists (select e_id 
                  from employee 
                  group by e_id 
                  having count(*)>=1);

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:

update employee set e_id='0132' where
concat_ws(',',name,ph_no,pay_scale)=concat_ws(',','AMIT','123456','PL-10') and 
e_id not in 
    (select e_id from 
    (select e_id from employee group by e_id having count(*)>=1) as t);

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

Query OK, 0 rows affected (0.01 sec)
Rows matched: 0  Changed: 0  Warnings: 0

also tried the below code:

update employee set 
employee.e_id='0132' where 
employee.e_id not in (select * from
    (select f.e_id from 
    employee f inner join employee b on 
    b.name=f.name and b.ph_no=f.ph_no and b.pay_scale=f.pay_scale) as tmp) 
and employee.name='AMIT' and employee.ph_no='123456' and employee.pay_scale='PL-10';

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…

update
  employee
set
  e_id='0132'
where
     name      = 'AMIT'
 and ph_no     = '123456'
 and pay_scale = 'PL-10'
 and e_id      not in (select e_id from 
                        (select distinct e_id
                           from employee
                          where e_id IS NOT NULL
                        )
                        as t
                       );

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

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