简体   繁体   中英

Mysql INSERT / SELECT based on Join?

INSERT INTO Comment (info, pID, cID) VALUES(?,?,?) 
SELECT Comm.cID
FROM Professor P, Comment Comm, Course Cou 
WHERE P.pID = Comm.pID
AND Cou.cID = Comm.cID;

So, basically im trying to insert the fields info, pID, and cID, where pID and cID are already populated, but cID is being pulled from the Comment table.

Is this possible? If so, whats wrong with my syntax.

Much appreciated.

Are you really talking about updating rows?

UPDATE Comment 
SET info = something
FROM Professor P, Comment Comm, Course Cou 
WHERE P.pID = Comm.pID
AND Cou.cID = Comm.cID;

Alternatively if you are simply making new Comment rows derived from the existing data (why?):

INSERT INTO Comment (info, pID, cID)
SELECT something /* what'sinfo */, P.pID, Comm.cID
FROM Professor P, Comment Comm, Course Cou 
WHERE P.pID = Comm.pID
AND Cou.cID = Comm.cID;

you need to have one value in each of the select positions that corresponds to the insert statement.

in your example, you only pull in one value, and are trying to populate 3 values.

also, you do not need the VALUES() clause when you use the SELECT syntax.

insert into ABC( a,b,c )
select 1,'bbb', 'ccc' from dual

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