因此,您需要添加一个WHERE
子句来限制foo
修改哪些行。
UPDATE
foo f
INNER JOIN
bar b
ON
b.fooId = f.Id
SET
f.something1 = b.barSome1,
f.something2 = b.barSome1,
f.something3 = b.barSome1,
f.something4 = b.barSome1
WHERE
/* only the latest foo.Id */
f.Id = (SELECT MAX(Id) FROM foo)
/* Or f.barSome1 is NULL or an empty string */
OR (f.barSome1 IS NULL OR f.barSome1 = '')
要做到这一点,最新post_date
的使用如下构造:
WHERE
f.id = (SELECT id FROM (SELECT id, post_date FROM foo HAVING post_date = MAX(post_date)) AS maxpost)
OR (f.barSome1 IS NULL OR f.barSome1 = '')
更新
要从 的单行修改foo
具有匹配条件的所有行,您可以对不带子句bar
的子查询使用联接。ON
首先使用 SELECT 语句对此进行测试。 我从来没有尝试过这样的事情,如果它破坏了你的foo
桌子,我不承担任何责任。:) 先备份。
测试 JOIN
SELECT f.*, barsub.*
FROM
foo f
JOIN (SELECT * FROM bar WHERE Id = (SELECT MAX(Id) FROM bar)) barsub
UPDATE 应该以同样的方式工作
UPDATE foo f
/* subquery returns one row so all rows of foo are joined to it */
JOIN (SELECT * FROM bar WHERE Id = (SELECT MAX(Id) FROM bar)) barsub
/* no ON clause */
SET
f.something1 = barsub.barSome1,
f.something2 = barsub.barSome1,
f.something3 = barsub.barSome1,
f.something4 = barsub.barSome1