我有2张桌子,CARS和ORDERS。
两者都有里程列,但ORDERS表里程列在大多数行上都是空白的。
我想用CARS.mileage更新ORDERS.mileage。它们由Vrm列链接。
有人可以帮我解决这个问题吗?
UPDATE orders
SET Mileage = (SELECT *
FROM `orders`
JOIN cars ON orders.Vrm = cars.Vrm
WHERE orders.mileage = '')
答案 0 :(得分:2)
在JOIN
的{{1}}子句中使用UPDATE
,而不是尝试使用子查询。否则,你基本上都有正确的想法。
FROM
答案 1 :(得分:0)
查询1:
UPDATE orders
SET Mileage = (SELECT c.mileage
FROM cars c
WHERE orders.Vrm = c.Vrm)
WHERE o.mileage = ''
查询2:
UPDATE orders o
JOIN cars c ON o.Vrm = c.Vrm
SET o.Mileage = c.mileage
WHERE o.mileage = ''
答案 2 :(得分:0)
根据MySQL documentation,你应该使用这个:
UPDATE orders, cars
SET orders.Mileage = cars.mileage
WHERE orders.Vrm = cars.Vrm AND orders.mileage = ''