Disallow using same target table in update clause in MySQL
P粉982009874
P粉982009874 2024-04-02 10:49:18
0
1
349

I have a table named "employee". The table creation code is as follows:

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

The contents of the table are as follows:

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 if there is the same e_id in the table, if it is not in the table then it will only update the row with the given e_id, otherwise it will not update. So my upgrade query is as follows:

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 gives the following error:

Error 1093 (HY000): You cannot specify the target table "employee" to update in the FROM clause I have tried the following 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 fails to update the table and displays the following results:

Query OK, 0 rows affected (0.01 sec)
匹配的行:0 更改:0 警告:0

Also tried the following 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 fails to update the table and gives the following results: Query OK, 0 rows affected (0.00 seconds) Matched lines: 0 Changes: 0 Warnings: 0 please help. Thank you in advance.

P粉982009874
P粉982009874

reply all(1)
P粉604669414

NULL plays differently than some people expect NOT IN: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=24c176ff4d4e2c52309aaca14cc121c5 So just ask WHERE e_id IS NOT NULL in the sub. Also, HAVING COUNT(*) >= 1 can be removed since it always returns 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 一个>

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!