我有一个名为“employee”的表。表创建代码如下:
create table employee(name varchar(50),ph_no varchar(10),e_id varchar(5),pay_scale varchar(5),year varchar(4));
表格内容如下:
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 |
+------+--------+------+-----------+------+
现在我想更新'e_id',首先它会检查表中是否有相同的e_id,如果不在表中那么它只会更新给定e_id的行,否则不会要去更新了。 因此,我的升级查询如下:
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);
但它给出了以下错误:
错误 1093 (HY000):您无法在 FROM 子句中指定要更新的目标表“employee” 我尝试过以下查询:
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);
但这也无法更新表格并显示以下结果:
Query OK, 0 rows affected (0.01 sec) 匹配的行:0 更改:0 警告:0
还尝试了以下代码:
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';
但这也无法更新表格并给出以下结果: 查询正常,0 行受影响(0.00 秒) 匹配的行:0 更改:0 警告:0 请帮忙。预先感谢您。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
NULL的播放方式与某些人期望的NOT IN不同:https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=24c176ff4d4e2c52309aaca14cc121c5 因此,只需将WHERE e_id IS NOT NULL放在子中询问。另外,HAVING COUNT(*) >= 1可以删除,因为它总是返回 1 或更多的值...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 一个>