LAST_INSERT_ID() MySQL

后端 未结 11 2407
花落未央
花落未央 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:11

    This enables you to insert a row into 2 different tables and creates a reference to both tables too.

    START TRANSACTION;
    INSERT INTO accounttable(account_username) 
        VALUES('AnAccountName');
    INSERT INTO profiletable(profile_account_id) 
        VALUES ((SELECT account_id FROM accounttable WHERE account_username='AnAccountName'));
        SET @profile_id = LAST_INSERT_ID(); 
    UPDATE accounttable SET `account_profile_id` = @profile_id;
    COMMIT;
    

提交回复
热议问题