Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5995769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:00:29+00:00 2026-05-23T00:00:29+00:00

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

  • 0

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 is the query

SELECT LAST_INSERT_ID();

but i’m not sure if it will work with insert ignore

what is the best way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T00:00:30+00:00Added an answer on May 23, 2026 at 12:00 am

    The documentation for LAST_INSERT_ID() says:

    If you use INSERT IGNORE and the row is ignored, the AUTO_INCREMENT counter is not incremented and LAST_INSERT_ID() returns 0, which reflects that no row was inserted.

    Knowing this, you can make this a multi-step process:

    • INSERT IGNORE
    • if LAST_INSERT_ID(), then done (new row was inserted)
    • else SELECT your_primary key FROM yourtable WHERE (your inserted data’s UNIQUE constraints)

    Example with U.S. states:

    id  | abbrev | other_data
     1  | AL     | ...
     2  | AK     |
    
    UNIQUE KEY abbr (abbrev)
    

    Now, inserting a new row:

    INSERT IGNORE INTO `states` (`abbrev`,`other_data`) VALUES ('AZ','foo bar');
    > OK
    SELECT LAST_INSERT_ID();
    > "3"
    // we have the ID, we're done
    

    Inserting a row which will be ignored:

    INSERT IGNORE INTO `states` (`abbrev`,`other_data`) VALUES ('AK','duplicate!');
    > OK
    SELECT LAST_INSERT_ID();
    > "0"
    // oops, it already exists!
    SELECT id FROM `states` WHERE `abbrev` = 'AK'; // our UNIQUE constraint here
    > "2"
    // there we go!
    

    Alternately, there is a possible workaround to do this in one step – use REPLACE INTO instead of INSERT IGNORE INTO – the syntax is very similar. Note however that there are side effects with this approach – these may or may not be important to you:

    • REPLACE deletes+recreates the row
      • so DELETE triggers are, um, triggered
      • also, the primary ID will be incremented even if the row exists
      • INSERT IGNORE keeps the old row data, REPLACE replaces it with new row data
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am running the following query in MySQL REPEAT INSERT IGNORE INTO tableB SELECT
I'm using JDBC to insert a row into a MYSQL database. I build a
After running a insert or update query against a SQLServer 2005 database, under what
$q = $db->prepare('INSERT IGNORE INTO Cities (Name,Stamp) VALUES (?,?)'); $q->execute(array($city,$stamp)); I am running this
I'm trying to insert text into a MySQL DataBase from a form using PHP.
I'm running an SQL query against a mainframe DB2 database using the OdbcDataReader class
I'm having trouble running a complex query against our company LDAP server. I'm using
I got on my php script $result1 = mysql_query(insert ignore into...this.... $result2 = mysql_query(insert
I want to make a simple news system using PHP and MySQL, right now
I'm running the following line : mysql_query(INSERT INTO tags SET tag = '.$onesearch.', SET

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.