mysql - 根据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
.....
我想在摄政时更新客户,所以结果如下所示。我已经使用update
withinner 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
,你可以试试这个。
更新 [表]
加入 [表] ...
设置 [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
推荐阅读
- php - 我在一个 Laravel 项目中。我在 Http/Controller 创建了一个 PagesController。我试图在此页面和 routes/web.php 之间创建一个链接
- javascript - 如何使用jsdoc声明返回值包含哪些属性?
- azure - 在为 azure AD 生成令牌时如何在 api 中传递范围
- python - 在 Python 中的每一行代码之后检查条件
- java - 我如何将任何社交媒体应用加入我的应用,而不仅仅是电子邮件
- php - PHP使用appkey解密加密的sek
- java - 有没有办法在 Selenium(java) 的 chrome 浏览器中输入非 URL 值(没有协议 http/https 的 URL)到地址栏?
- javascript - 异步函数在同步函数内返回
- user-interface - Javafx 使 gui 显示与底层数据保持同步
- api - 如何找出在信任平台中创建身份时可以使用的可用上下文链接?