Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. How do I get all the Ids from my last insert in Mysql?

How do I get all the Ids from my last insert in Mysql?

Scheduled Pinned Locked Moved Database
databasequestionmysqltutorial
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    karengsh
    wrote on last edited by
    #1

    Hi, I am using MySQL. Can someone tell me how to write the sql query for retrieving all the last inserted Ids (plural) ? I have a table in which a column has been inserted by serveral rows everytime depending on how many subject the users tick in the checkboxes. Now, I have to get all the auto-incremental Ids from all the Ids that have just been inserted. Is there a way to do it via sql query ?

    L Richard DeemingR Z 3 Replies Last reply
    0
    • K karengsh

      Hi, I am using MySQL. Can someone tell me how to write the sql query for retrieving all the last inserted Ids (plural) ? I have a table in which a column has been inserted by serveral rows everytime depending on how many subject the users tick in the checkboxes. Now, I have to get all the auto-incremental Ids from all the Ids that have just been inserted. Is there a way to do it via sql query ?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      If you know the number of rows that you want: Mysql: Select top N max values? - Stack Overflow[^].

      K 1 Reply Last reply
      0
      • L Lost User

        If you know the number of rows that you want: Mysql: Select top N max values? - Stack Overflow[^].

        K Offline
        K Offline
        karengsh
        wrote on last edited by
        #3

        Hi Richard, I won't be able to know because each time the subject_Ids varies, it is dependent on the user's no of checkboxes that have been ticked. So, how ?

        L 1 Reply Last reply
        0
        • K karengsh

          Hi, I am using MySQL. Can someone tell me how to write the sql query for retrieving all the last inserted Ids (plural) ? I have a table in which a column has been inserted by serveral rows everytime depending on how many subject the users tick in the checkboxes. Now, I have to get all the auto-incremental Ids from all the Ids that have just been inserted. Is there a way to do it via sql query ?

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          In MS SQL Server, you'd use the OUTPUT clause. Unfortunately, there doesn't seem to be an equivalent for MySQL. Selecting the top N rows won't work - another user might have inserted rows between your INSERT and SELECT statements. That's why the LAST_INSERT_ID function exists. MySQL :: MySQL 5.7 Reference Manual :: 27.8.15.3 How to Get the Unique ID for the Last Inserted Row[^] I suspect you might need to insert the values one at a time, and use LAST_INSERT_ID to retrieve the ID.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          K 1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            In MS SQL Server, you'd use the OUTPUT clause. Unfortunately, there doesn't seem to be an equivalent for MySQL. Selecting the top N rows won't work - another user might have inserted rows between your INSERT and SELECT statements. That's why the LAST_INSERT_ID function exists. MySQL :: MySQL 5.7 Reference Manual :: 27.8.15.3 How to Get the Unique ID for the Last Inserted Row[^] I suspect you might need to insert the values one at a time, and use LAST_INSERT_ID to retrieve the ID.


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            K Offline
            K Offline
            karengsh
            wrote on last edited by
            #5

            But, how do I insert one at a time when my situation requires me to insert by batch ?

            1 Reply Last reply
            0
            • K karengsh

              Hi Richard, I won't be able to know because each time the subject_Ids varies, it is dependent on the user's no of checkboxes that have been ticked. So, how ?

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              You will have to use some other logic to find out how many new records have been added. Possibly by finding the highest ID number before you start.

              1 Reply Last reply
              0
              • K karengsh

                Hi, I am using MySQL. Can someone tell me how to write the sql query for retrieving all the last inserted Ids (plural) ? I have a table in which a column has been inserted by serveral rows everytime depending on how many subject the users tick in the checkboxes. Now, I have to get all the auto-incremental Ids from all the Ids that have just been inserted. Is there a way to do it via sql query ?

                Z Offline
                Z Offline
                ZurdoDev
                wrote on last edited by
                #7

                You can add a timestamp or guid column to mark them. Essentially, you need to have your own logic.

                There are two kinds of people in the world: those who can extrapolate from incomplete data. There are only 10 types of people in the world, those who understand binary and those who don't.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups