gpt4 book ai didi

mysql - 根据mysql的另一个表(4个表)值更新表

转载 作者:行者123 更新时间:2023-11-30 21:44:13 24 4
gpt4 key购买 nike

我有4张 table

第一个表:customer(大约2000年的记录)

ID      province      regency     district
1 CJ SE T district
2 CJ SE G district
3 CJ SE B district
.....

第二张表:省份

ID      province      province_id
1 CJ 33
2 WJ 32
3 EJ 31
.....

第三张 table :摄政

ID      regency        province_id
3301 SE city 33
3302 SE regency 33
3303 SK city 33
3304 SK regency 33
.....

第四张表:分区

ID      district       regency_id
3301 T district 3301
3302 G district 3301
3303 A district 3302
3304 B district 3302
.....

我想更新 regency 的客户,所以结果如下所示。我已经将 updateinner join 等一起使用,但没有我需要的结果。

ID      province      regency      district
1 CJ SE city T district
2 CJ SE city G district
3 CJ SE regency B district
.....

我已经使用了这个查询,但它根本不起作用。结果什么都没有

UPDATE customer
inner JOIN province ON customer.province = province.`name`
inner join district on customer.district = district.`name`
SET customer.regency = regencie.`name`
WHERE
customer.regency = province.`name`
AND customer.district = district.`name`

非常感谢。如果有已经解决的类似问题的帖子,请纠正我。

最佳答案

如果你想用JOIN更新,你可以试试这个。

UPDATE [table]

JOIN [table] ...

SET [col] = [col1]

Where ...

UPDATE customer c 
inner join province p on p.province = c.province
inner join regency r on r.province_id = p.province_id
SET c.regency = r.regency

SQLFIDDLE:http://sqlfiddle.com/#!9/3b0cb6/1

编辑

根据您的评论,您需要加入 district table link by

像这样。

UPDATE customer c 
inner join province p on p.province = c.province
inner join regency r on r.province_id = p.province_id
inner join district d on d.regency_id = r.ID
SET c.regency = r.regency
where c.district = d.district

sqlfiddle:http://sqlfiddle.com/#!9/5dc7b2/1

关于mysql - 根据mysql的另一个表(4个表)值更新表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50198582/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com