简体   繁体   中英

mysql update query from another table

I want to update some fields in my products table from a temporary table called updated table.

So far I have this, but I have an sql error, I cant find what it is yet though.

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

ANSI solution:

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 solution:

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

If you are working on replication mysql servers, beware of prediction errors on combined updates.

Quicker version :

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

there is some problem with your query . try following query :

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

As far as I know, Syntax of Update is,

UPDATE tablename SET columnname=value WHERE condition;

not

UPDATE tablename SET columnname=value FROM something;

Rather try,

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

or

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM