I have code like this:
require 'mysql2'
@db.query("insert into clients (Name) values ('#{client}')")
Can I return last inserted id with 1 query?
You can use last_id
method of the client instance:
client = Mysql2::Client.new(:host => "localhost", :username => "root", :password => "", :database => "db")
insert = client.query "INSERT INTO test (name, date) VALUES ('name', '2011-11-28 09:13:56')"
id = client.last_id
http://rubydoc.info/gems/mysql2/0.2.6/Mysql2/Client:last_id
Edit: it doesn't answer your original question but still looks better than call for LAST_INSERT_ID
I don't anything about gem but you can try to run
@db.query("SELECT LAST_INSERT_ID();")
after your first query.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With