mysql - 基于3个表进行更新

时间:2017-04-30 14:08:08

标签: mysql

我尝试同时更新3个表。 clients.id> cars.client_id> works.car_id是无法实现的

UPDATE clients, cars, works 
JOIN cars on cars.client_id = clients.id 
JOIN works on works.car_id = cars.id 
SET clients.name = '15-adsadadadsa' , 
clients.email = '15-aaa@aa.com' , 
clients.phone = '15-3232131', 
clients.email = '15-aaa@aa.com' , 
cars.reg = 'aaa-aaa' , 
cars.model = 'aaa-aaa' , 
cars.year = '111111' , works.work = 'aaaa' WHERE works.id = '15';

但是得到错误

不唯一的表/别名:'cars'

谢谢你的帮助!

1 个答案:

答案 0 :(得分:1)

这是因为x[a][t] == 1表被提及两次(在cars以及UPDATE中),如果它们是JOIN,则无需提及UPDATE中的表名称在JOIN中,试试这个:

UPDATE clients
JOIN cars on cars.client_id = clients.id 
JOIN works on works.car_id = cars.id 
SET clients.name = '15-adsadadadsa' , 
clients.email = '15-aaa@aa.com' , 
clients.phone = '15-3232131', 
clients.email = '15-aaa@aa.com' , 
cars.reg = 'aaa-aaa' , 
cars.model = 'aaa-aaa' , 
cars.year = '111111' , works.work = 'aaaa' WHERE works.id = '15';