View Single Post

   
  #3 (permalink)  
Old 02-28-2008, 10:07 AM
Willem Bogaerts
 
Posts: n/a
Default Re: Can I determine the index (value) of an auto-increment field

> (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
--
Willem Bogaerts

Application smith
Kratz B.V.
http://www.kratz.nl/
Reply With Quote