简体   繁体   English

如何通过设置另一个表中的值来创建要插入到表中的MySQL语句

[英]How to create MySQL statement to insert into a table by setting values from another table

I need to insert rows into a table using values from another table. 我需要使用另一个表中的值将行插入表中。

The following statement is approx what I want, I just can't figure out how to do the SELECT. 下面的语句是我想要的,我只是想不通如何执行SELECT。

INSERT INTO forumlink
SET 
    forumlink.personid = fp.posterid, 
    forumlink.link = '/app-views/forum/post-view?postID=' + fp.postid,
    forumlink.type = 'MYPOSTS'
(SELECT posterid, postid FROM forumpost fp);

Thanks for helping. 感谢您的帮助。

BobC BOBC

You will need to add the fixed values to the ones you are selecting from the other table and use an INSERT ... SELECT query: 您需要将固定值添加到要从另一个表中选择的值,然后使用INSERT ... SELECT查询:

INSERT INTO forumlink (personid, link, type)
SELECT posterid, CONCAT('/app-views/forum/post-view?postID=', postid), 'MYPOSTS'
FROM forumpost

It's simpler than you're making it: 它比您做的要简单:

INSERT INTO forumlink (personid, link, type)
SELECT fp.posterid, CONCAT('/app-views/forum/post-view?postID=', fp.postid), 'MYPOSTS'
FROM forumpost fp;

You can read more about the syntax in the relevant documentation . 您可以在相关文档中阅读有关语法的更多信息。

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

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