-2

Pleas see it: (oid, iid, name, type) VALUES (5, X3, john, boxer) I want to insert oid. It is from a different table $oid=mysql_fetch_array['oid']; where iid name and type is same. Can i insert thes at only one statement

Fujael
  • 35
  • 5

2 Answers2

6

An INSERT query may have many sets of values:

INSERT INTO table (oid, iid, name, type) VALUES (5, 'X3', 'john', 'boxer'), (8, 'X3', 'john', 'boxer'), (10, 'X3', 'john', 'boxer')...

http://dev.mysql.com/doc/refman/5.5/en/insert.html

You will need to specify all values for all columns you wish to insert even if they are the same in multiple rows.

Dan Grossman
  • 51,866
  • 10
  • 112
  • 101
  • +1. I don't mind seeing this answer. I learnt a new thing today because of it. @mmmshuddup You're right on your grounds, but as you see there are advantages too – asprin Nov 21 '12 at 06:48
  • Yeah. I was going to answer this question but then I said "no, it really is totally an _exact_ duplicate of another question." ha oh well. – Yes Barry Nov 21 '12 at 06:50
  • Do you know how to make loop for specifying values as for($i=0; $i=<10 $i++){ code for inserting values} – Fujael Nov 21 '12 at 07:14
  • @Fujael If you do not know how to write loops and create strings, you need to buy a book. That is too basic to ask for help with. – Dan Grossman Nov 21 '12 at 09:38
3
$query = "INSERT INTO your_table
            (oid, iid, name, type)
          VALUES
            (5, 'X3', 'john', 'boxer'),
            (8, 'X3', 'john', 'boxer'),
            (10, 'X3', 'john', 'boxer'),
            (11, 'X3', 'john', 'boxer'),
            (60, 'X3', 'john', 'boxer'),
            (220, 'X3', 'john', 'boxer'),
            (311, 'X3', 'john', 'boxer'),
            (336, 'X3', 'john', 'boxer'),
            (339, 'X3', 'john', 'boxer'),
            (800, 'X3', 'john', 'boxer');";
$result = mysql_query($query);
Tahir Yasin
  • 11,489
  • 5
  • 42
  • 59