简体   繁体   中英

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.

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

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

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