mysql更新来自另一个表的查询

时间:2013-07-11 09:34:16

标签: mysql

我想从名为updated table的临时表中更新product表中的某些字段。

到目前为止,我有这个,但我有一个SQL错误,但我无法找到它现在。

UPDATE destination 

SET destination.title = source.title, 
destination.title_tag = source.title_tag,
destination.keywords = source.keywords

FROM updated_table AS source

JOIN products AS destination ON source.id = destination.id

5 个答案:

答案 0 :(得分:1)

UPDATE products AS destination
JOIN updated_table AS source
ON source.id = destination.id
SET destination.title = source.title, 
destination.title_tag = source.title_tag,
destination.keywords = source.keywords

答案 1 :(得分:0)

ANSI解决方案:

UPDATE
 products destination
SET
 destination.title = (SELECT title FROM updated_table source WHERE destination.id = source.id),
 destination.title_tag = (SELECT title_tag FROM updated_table source WHERE destination.id = source.id),
 destination.keywords = (SELECT keywords FROM updated_table source WHERE destination.id = source.id)

MySQL解决方案:

UPDATE
 products destination
JOIN
 updated_table source
ON
 source.id = destination.id
SET
 destination.title = source.title, 
 destination.title_tag = source.title_tag,
 destination.keywords = source.keywords

如果您正在处理复制mysql服务器,请注意组合更新的预测错误。

答案 2 :(得分:0)

更快的版本:

UPDATE destination AS source, products AS destination
SET destination.title = source.title, 
destination.title_tag = source.title_tag,
destination.keywords = source.keywords
where source.id = destination.id

答案 3 :(得分:-1)

您的查询存在一些问题。尝试以下查询:

UPDATE destination 

SET title = source.title, 
title_tag = source.title_tag,
keywords = source.keywords

FROM updated_table AS source

JOIN products AS destination ON source.id = destination.id

答案 4 :(得分:-1)

据我所知,更新的语法是,

UPDATE tablename SET columnname=value WHERE condition;

UPDATE tablename SET columnname=value FROM something;

相反,尝试,

UPDATE products AS destination JOIN updated_table AS source 
ON source.id = destination.id
SET destination.title = source.title, 
destination.title_tag = source.title_tag,
destination.keywords = source.keywords

UPDATE products AS destination, updated_table AS source 
SET destination.title = source.title, 
destination.title_tag = source.title_tag,
destination.keywords = source.keywords 
WHERE source.id = destination.id