To save the last id inserted in a SQL variable , we need to do this:
INSERT INTO Users (user_name, user_email, user_address, user_city)
VALUES ('bryan.hamilton','Tbha@gmail.com','Av.21','ohio');
//we create a new variable called userLastId
SET @userLastId = LAST_INSERT_ID( );
//now, we can use this varable in another sql sentence
INSERT INTO articles (art_title, art_body, art_user_id, art_created_at)
VALUES ('Geologic time scale','my body ..',@userLastId ,NOW());
Basically, when you save the last id inserted in the variable that value is available to use it in others SQL sentences.
No comments:
Post a Comment