Re: Mutiple inserts with one query?
| From: | Frank Flynn | Date: | Sat, 01 Jun 2002 01:58:51 +0000 |
| Subject: | Re: Mutiple inserts with one query? | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-19565@lists.php.net to get a copy of this message | ||
Yes there is, it's kind of a hack...
If you insert using "values" you can only insert 1 row:
INSERT foo (col1, col2, col3)
VALUES("Moe", "Larry", "Curley")
But if you use a select statement it will insert as many rows as the select
returns:
INSERT foo (col1, col2, col3)
SELECT col1, col2, col3
FROM bar
WHERE ...
So the hack is to be able to insert static values (OK PHP Variables - but to
the query they look static) into a table as often as you like. Create a new
table "Counter" with one integer field "count", we'll have the integers in
it 1, 2, 3, 4, ... Up to the maximum you think you'll want to insert.
Now you can combine the two methods to get what you were after:
INSERT foo (col1, col2, col3)
SELECT "Moe", "Larry", "Curley" /* substitute your PHP variables
here */
FROM Counter
WHERE count <= $numOfRowsYouWanted
OK? Now why are you doing this in the first place? It violates one of the
rules of good DB design (having identical information in more than one
place). I'll bet there's a better way to do what you're trying to do here.
Frank
On 5/31/02 12:13 PM, "php-db-digest-help@lists.php.net"
<php-db-digest-help@lists.php.net> wrote:
> From: Leif K-Brooks <eurleif@buyer-brokerage.com>
> Date: Fri, 31 May 2002 15:12:21 -0400
> To: php-db@lists.php.net
> Subject: Mutiple inserts with one query?
>
> Is there a way to insert multiple identical rows in one mysql query?
> I'm using a looped query right now, which uses more server load than it
> should... Thanks!
>