Skip to content
Advertisement

How to insert to a table if there exist a value in another table

i have tried this

INSERT into examqst set qno=1, qst="aa" ,qan1="a" , qan2="B", qan3="C", qan4="d", qant="A", qtype=0, examid=1 
WHERE EXISTS (SELECT * FROM examname  where examid=1 and s_id=10); 

I want to insert to examqst only if there is a row examid in examname table and s_id in examname have a perticilar value

Advertisement

Answer

You could an INSERT INTO...SELECT:

INSERT INTO examqst (qno, qst, qan1, qan2, qan3, qan4, qant, qtype, examid)
SELECT 1, 'aa', 'a', 'B', 'C', 'd', 'A', 0, 1 
WHERE EXISTS (SELECT 1 FROM examname WHERE examid = 1 AND s_id = 10);
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement