Home > Software design >  How to insert to a table if there exist a value in another table
How to insert to a table if there exist a value in another table

Time:09-24

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

CodePudding user response:

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);
  • Related