Mysql insert into 2 tables

后端 未结 4 485
予麋鹿
予麋鹿 2020-11-27 08:00

I want to make a insert into 2 tables

visits:

visit_id int | card_id int

registration:

registration_id int | type          


        
4条回答
  •  失恋的感觉
    2020-11-27 08:24

    It seems like the problem you are trying to solve is to get the auto-increment value from the "visits" row to insert into "registration". Am I right?

    If so, you can just use the LAST_INSERT_ID() function like this:

    INSERT INTO `visits` (`visit_id`,`card_id`) 
    VALUES (NULL, 12131141);
    INSERT INTO `registration` (`registration_id`, `type`, `timestamp`, `visit_id`) 
    VALUES (NULL, 'in', UNIX_TIMESTAMP(), LAST_INSERT_ID());
    

提交回复
热议问题