更新正在查询的同一个表

时间:2017-11-07 10:02:39

标签: mysql sql

此查询按预期工作,并返回缺少的客户编号。

SELECT *
FROM Person_Details AS b
     INNER JOIN Person_Details AS a ON a.holdername = b.holdername
                                       AND a.District = b.District
                                       AND a.Dob = b.DOb
                                       AND a.Address = b.Address
WHERE b.customer_No IS NULL limit 10

我需要做的是找到给定“holdernames”的客户编号,并在任何地方更新为NULL。我希望以下更新能够正常运行。但它更新了0条记录。

update Person_Details as b inner join Person_Details as a
on a.holdername = b.holdername  and a.District = b.District   and a.Dob = b.DOb and a.Address = b.Address 
set b.customer_no = a.customer_No 
where b.customer_No is null

我是否正确使用自我加入更新?

更新:

当我尝试使用测试用例时,它按预期工作。但是当我在生产服务器上使用相同的查询时,它不会更新任何行。我只是想知道我是否遗漏了一些显而易见的东西。

create table Person_Details (holdername varchar(100), District varchar(100),
                             Dob varchar(100), Address varchar(100), customer_No int);

                             insert into Person_Details values ('shantanu', 'mumbai', '1970-11-16', 'india', 123);
                             insert into Person_Details values ('shantanu', 'mumbai', '1970-11-16', 'india', NULL);
                             insert into Person_Details values ('akbar', 'delhi', '1995-12-11', 'india', 987);


select * from  Person_Details as b inner join Person_Details as a
on a.holdername = b.holdername  and a.District = b.District  and a.Dob = b.DOb and a.Address = b.Address 
where b.customer_No is null
limit 10;

update Person_Details as b inner join Person_Details as a
on a.holdername = b.holdername  and a.District = b.District   and a.Dob = b.DOb and a.Address = b.Address 
set b.customer_no = a.customer_No 
where b.customer_No is null

0 个答案:

没有答案