简体   繁体   English

如何从另一个表更新MYSQL中的一个表? 具有额外的价值

[英]How to update one table in MYSQL from another table? with extra values

I have two tables that are similar. 我有两个相似的表。

Table word
column names: id, name, describe
1 bob Is a cat
2 Sam Not a giraffe
3 Gub Like a cat

Table temp
column names: id, name, describe
1 bob Is a cat
2 Sam Not a giraffe
3 Gub Like a cat
4 Col The other one

I am currently using this to update table word from table temp, but the 4th value isn't being added. 我目前正在使用它从表temp更新表字,但是未添加第4个值。 How would I do that? 我该怎么做?

UPDATE temp p, word pp
SET pp.name= p.name, pp.describe = p.describe
WHERE pp.id = p.id;

Try this :: INSERT ON DUPLICATE KEY UPDATE 试试这个::插入重复的密钥更新

INSERT INTO word(id, name, describe) SELECT id, name, describe FROM temp 
ON DUPLICATE KEY UPDATE SET word.name = temp.name, word.describe = temp.describe;

If I understand correctly, you want to copy values from one table to another, keeping the same id . 如果我理解正确,则希望将值从一个表复制到另一个表,并保持相同的id In this case, you can't put condition WHERE pp.id = p.id. 在这种情况下,您不能将条件WHERE pp.id =p.id。 This way, it would only copy rows with id that already exist in 'word' table, skipping any unique rows. 这样,它将仅复制“单词”表中已存在的具有id行,并跳过所有唯一行。 You can use "INSERT INTO...ON DUPLICATE KEY UPDATE" to get arround it. 您可以使用“ INSERT INTO ... ON DUPLICATE KEY UPDATE”来解决它。 http://dev.mysql.com/doc/refman/5.1/en/insert-on-duplicate.html http://dev.mysql.com/doc/refman/5.1/en/insert-on-duplicate.html

the last one would need an insert statement because it does not exist yet 最后一个需要插入语句,因为它尚不存在

INSERT INTO word pp SELECT * FROM temp left join word on on temp.id = word.id where temp.id IS NULL 将单词INSERT INTO到pp SELECT中*从temp左连接temp.id = word.id的单词,其中temp.id为NULL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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