Extracting and inserting id when querying the database

There is a database where id is AUTOINCREMENT. How to make a query in such a way that this id, which was, would be pulled out by auto-increment and inserted into another field in the same table? The picture is clearer. The id and weight field.

alt text


Answer 1, authority 100%

UPDATE tab SET weight = id
WHERE weight= 0

Answer 2

mysql_insert_id()returns 0 if the previous query did not generate an AUTO_INCREMENT value. If you need to store a value for the future, call mysql_insert_id() right after the query that generates the value.


Answer 3

INSERT INTO table VALUE (NULL, 0, 0);
UPDATE table SET weight = LAST_INSERT_ID() WHERE id = LAST_INSERT_ID();

Answer 4

Do you mean that when adding a newrecord, immediately write the ID of this record in some field? If yes, then you can find out this ID before the INSERT query

$res_id = mysql_query("SHOW TABLE STATUS LIKE 'table_name';");
$row_id = mysql_fetch_assoc($res_id);
$id = $row_id['Auto_increment'];

Leave a reply

Please enter your comment!
Please enter your name here