mysql - after insert ignore get primary key

后端 未结 2 566
忘掉有多难
忘掉有多难 2020-12-09 09:19

i am running a query in mysql insert ignore into........ using Python

after running the query I want to know the primary key of the row. I know there i

2条回答
  •  醉酒成梦
    2020-12-09 09:27

    Try using ON DUPLICATE KEY instead of INSERT IGNORE, maybe this can work for you:

    INSERT INTO your_table (`id`,`val`) VALUES(1,'Foo') ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(`id`);
    
    SELECT LAST_INSERT_ID();
    

    Also see related question: MySQL ON DUPLICATE KEY - last insert id?

提交回复
热议问题