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 6821557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:33:32+00:00 2026-05-26T21:33:32+00:00

Pretty straight forward problem. I simply want to return the affected row created by

  • 0

Pretty straight forward problem. I simply want to return the affected row created by a SQL INSERT query so I can get data from it.

There’s a way I can do it already, I suppose, but I’m hoping the Mysql or Mysql2 gem provides some mechanism for me not having to make the second SELECT query.

The solution I’m leaning towards right now is something akin to:

"INSERT INTO table (col1) VALUES ('value');"

and then:

"SELECT cid FROM table ORDER BY cid DESC LIMIT 1;"

Since cid is the auto-increment index of the table (it’s InnoDB fyi), it will always be the largest cid value in the table until you do another INSERT.

Is there any mechanism in Mysql or Mysql2 to avoid having to make that second SELECT query?

  • 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-26T21:33:33+00:00Added an answer on May 26, 2026 at 9:33 pm

    MySQL2 has a last_id method. The documentation for that method is worthless but the implementation looks like this:

    static VALUE rb_mysql_client_last_id(VALUE self) {
      GET_CLIENT(self);
      REQUIRE_OPEN_DB(wrapper);
      return ULL2NUM(mysql_insert_id(wrapper->client));
    }
    

    And the MySQL mysql_insert_id function does this:

    Returns the value generated for an AUTO_INCREMENT column by the previous INSERT or UPDATE statement.

    So you can do your INSERT and then get the last ID by calling the last_id method.

    And BTW, your current approach:

    SELECT cid FROM table ORDER BY cid DESC LIMIT 1;
    

    is not safe if you’re in a multiprocess environment, consider this:

    1. You INSERT a row.
    2. Another process INSERTs a row.
    3. You SELECT cid FROM table ORDER BY cid DESC LIMIT 1 and get the ID from (2).

    You could SELECT last_insert_id() though, last_insert_id() is session-specific so you don’t have to worry about other processes when using it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i think this is a pretty straight forward problem , but i still can
I thought this was pretty straight forward but I don't get the same results
I'm having a problem updating a table and im sure its pretty straight forward
This is a pretty straight-forward problem. I basically have a div block on a
The problem for me is pretty straight forward. Onclick of an anchor tag ,
This is a pretty straight forward attempt. I haven't been using python for too
This seems like it should be pretty straight forward, but I'm apparently confused. I
I'm currently working with Groovy and Grails. While Groovy is pretty straight-forward since it's
I have a side project I do = in Java. It's a pretty straight-forward
Well, first of all sorry about this question it must be pretty straight forward

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.