简体   繁体   中英

How do I combine mulitple INSERT queries for the same table

I have a list of questions answered on a form. These questions are input into the same fields over and over on the same MySQL table called submission_questions. Currently I am doing a query for each insertion and I'm sure there is a better way, I just haven't been able to find it. What I have so far is:

//Insert Department into database
$query_2 = "INSERT INTO submission_questions (submission_id, question_id, answer) VALUES ('" . $subid . "','4', '" . $dept . "')";
mysql_query($query_2) or die(mysql_error());

//Insert GPO Question into database
$query_3 = "INSERT INTO submission_questions (submission_id, question_id, answer) VALUES ('" . $subid . "','18', '" . $gpo . "')";
mysql_query($query_3) or die(mysql_error());

I could end up with 20 of these so I need to combine them but I am confused on how to do so since they are being put into the same field. I saw many posts on SELECT statements but nothing on INSERT statements. Can someone point me in the right direction?

Referencing to the manual which says: INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9)

PS. You don't have to enclose Int values into single quotes.

As far as I know, you can use code like the following to insert multiple records at once:

INSERT INTO example
  (example_id, name, value, other_value)
VALUES
  (100, 'Name 1', 'Value 1', 'Other 1'),
  (101, 'Name 2', 'Value 2', 'Other 2'),
  (102, 'Name 3', 'Value 3', 'Other 3'),
  (103, 'Name 4', 'Value 4', 'Other 4');

Hope this helps.

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