1

我有 2 张桌子,CARS 和 ORDERS。

两者都有一个 mileage 列,但 ORDERS 表的 mileage 列在大多数行上是空白的。

我想用 CARS.mileage 更新 ORDERS.mileage。它们由 Vrm 列链接。

有人可以帮我解决这个问题吗?

UPDATE orders
SET Mileage = (SELECT *
FROM `orders`
JOIN cars ON orders.Vrm = cars.Vrm
WHERE orders.mileage = '')
4

3 回答 3

2

JOINUPDATE'子句中使用FROMa,而不是尝试使用子查询。否则,您基本上有正确的想法。

UDPATE
  orders
  JOIN CARS ON orders.Vrm = cars.VRM
SET orders.Mileage = cars.mileage
WHERE orders.mileage = ''
于 2013-02-06T14:16:48.930 回答
0

查询一:

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 = ''

一些sqlfileexample

于 2013-02-06T15:18:08.337 回答
0

根据MySQL 文档,你应该使用这个:

UPDATE orders, cars
SET orders.Mileage = cars.mileage
WHERE orders.Vrm = cars.Vrm AND orders.mileage = ''
于 2013-02-06T15:26:41.233 回答