INSERT ...在mysql中的SELECT查询

时间:2012-07-22 11:14:44

标签: mysql sql

我有两张桌子:

`user1`
- full_name
- headline # this is empty

`user2`
- full_name
- headline # this has content

我想将headlineuser2表插入到user1表中。这就是我到目前为止所做的:

insert into user1 set headline = (select headline from user2 where headline=headline)

但是,我收到一条错误消息,说select会返回多行。我如何正确发出此插入语句?

2 个答案:

答案 0 :(得分:1)

目前还不完全清楚你想做什么。特别是,当每个表中有多个记录时会发生什么?如果您要将标题从user2复制到user1匹配的full_name,则可以使用多表UPDATE语法来连接表并更新{{1 }}:

user1

答案 1 :(得分:0)

如果你真的想要INSERT那么你想要

INSERT INTO user1 SELECT full_name, headline

如果你真的想要更新,那么你想要

UPDATE user1 SET headline = (SELECT headline FROM user2 WHERE user1.full_name = user2.full_name)