我有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
.....
我想在摄政时更新客户,结果如下所示。我已经将update
与inner 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`
非常感谢你。如果有类似问题的帖子已经解决,请纠正我。
答案 0 :(得分:0)
如果您想使用JOIN
进行更新,可以试试这个。
更新[table]
加入[表格] ......
SET [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