View Single Post

   
  #4 (permalink)  
Old 02-28-2008, 09:07 AM
Jerry Stuckle
 
Posts: n/a
Default Re: Can I determine the index (value) of an auto-increment field

Willem Bogaerts wrote:
>> (insert row)
>> $start = mysql_insert_id();
>> (insert more rows)
>> $result = mysql_query("UPDATE mytable " .
>> "SET col1=MOD(id, 30)" .
>> "WHERE id >= " . id);
>>

>
> Or, in one go, on the database server:
> INSERT INTO mytable(...) VALUES(...);
> SET @Id=LAST_INSERT_ID();
> UPDATE mytable SET col1=MOD(@Id, 30) WHERE id=@Id;
> -- More rows:
> INSERT INTO mytable(...) VALUES(@Id, ...);
>
>
> This should be done in one connection session, otherwise the variable
> @Id is cleared.
>
> Best regards


Yes, but it's less efficient than when updating multiple rows. Each
entry has an INSERT, a SET and an UPDATE. This requires three
statements to be parsed and executed for each insert - much slower,
unless you have a very slow database connection.

My way has one INSERT per row, on get last insert id and a single
UPDATE. Parsing/execution will be much faster.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Reply With Quote