LAST_INSERT_ID() MySQL

后端 未结 11 2471
花落未央
花落未央 2020-11-22 10:56

I have a MySQL question that I think must be quite easy. I need to return the LAST INSERTED ID from table1 when I run the following MySql query:

INSERT INTO          


        
11条回答
  •  时光说笑
    2020-11-22 11:17

    It would be possible to save the last_id_in_table1 variable into a php variable to use it later?

    With this last_id I need to attach some records in another table with this last_id, so I need:

    1) Do an INSERT and get the last_id_in_table1

    INSERT into Table1(name) values ("AAA"); 
    SET @last_id_in_table1 = LAST_INSERT_ID();
    

    2) For any indeterminated rows in another table, UPDATING these rows with the last_id_insert generated in the insert.

    $element = array(some ids)    
    foreach ($element as $e){ 
             UPDATE Table2 SET column1 = @last_id_in_table1 WHERE id = $e 
        }
    

提交回复
热议问题