首页 > 解决方案 > 根据mysql的另一个表(4个表)值更新表

问题描述

我有 4 张桌子

第一个表:客户(它的记录大约 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         
.....

第三张桌子:摄政

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         
.....

我想在摄政时更新客户,所以结果如下所示。我已经使用updatewithinner 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`

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

标签: mysqlsqljoin

解决方案


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

更新 [表]

加入 [表] ...

设置 [col] = [col1]

在哪里 ...

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

编辑

从您的评论中,您需要join通过district表格链接

像这样。

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


推荐阅读