last-insert-id

MySQL SELECT LAST_INSERT_ID() for compound key. Is it possible?

杀马特。学长 韩版系。学妹 提交于 2019-12-01 20:35:35
Can I get the LAST INSERT ID() for a compound key in MySQL? Yes. You can't have multiple auto-increment fields in a single table. CREATE TABLE foo ( id1 int(11) NOT NULL auto_increment, id2 int(11) NOT NULL default '0', PRIMARY KEY (id1, id2) ); INSERT INTO foo VALUES (DEFAULT, 2); SELECT LAST_INSERT_ID(); -- returns 1, the value generated for id1 LAST_INSERT_ID() returns the value only for a column declared AUTO_INCREMENT . There's no function to return the value in a compound primary key that wasn't generated by the system. You ought to know that value already, since you just gave it in an

LAST_INSERT_ID() always returns 0 (RMySQL) - separate connection issue

安稳与你 提交于 2019-12-01 18:08:58
Original example as found in some post According to this post the following SQL statements should give me a vector 1, 2, 2, 2, 2 in the end: require("RMySQL") con <- dbConnect( dbDriver("MySQL"), db="your_db", user="your_user", password="your_pw", host="localhost" ) > con <MySQLConnection:(6640,122)> > dbSendQuery(con, "DROP TABLE IF EXISTS t;") <MySQLResult:(6640,122,0)> > dbSendQuery(con, "CREATE TABLE t (i INT NOT NULL AUTO_INCREMENT PRIMARY KEY);") <MySQLResult:(6640,122,1)> > dbSendQuery(con, "INSERT INTO t VALUES(NULL);") <MySQLResult:(6640,122,2)> > dbGetQuery(con, "SELECT LAST_INSERT

LAST_INSERT_ID() always returns 0 (RMySQL) - separate connection issue

允我心安 提交于 2019-12-01 17:41:56
问题 Original example as found in some post According to this post the following SQL statements should give me a vector 1, 2, 2, 2, 2 in the end: require("RMySQL") con <- dbConnect( dbDriver("MySQL"), db="your_db", user="your_user", password="your_pw", host="localhost" ) > con <MySQLConnection:(6640,122)> > dbSendQuery(con, "DROP TABLE IF EXISTS t;") <MySQLResult:(6640,122,0)> > dbSendQuery(con, "CREATE TABLE t (i INT NOT NULL AUTO_INCREMENT PRIMARY KEY);") <MySQLResult:(6640,122,1)> > dbSendQuery

Using Mysql to do multiple INSERT on linked tables

只谈情不闲聊 提交于 2019-12-01 02:41:31
问题 I have two tables, one linked to the Primary Key of the other. At the moment I INSERT into table A, get the LAST_INSERT_ID, and then INSERT into table B. But I have hundreds of records to insert and I want to speed things up. In Mysql you can either: INSERT INTO table_a (v1, v2, c3) VALUE (0, 1, 2); INSERT INTO table_a (v1, v2, v3) VALUE (4, 5, 6); etc, or INSERT INTO table_a (v1, v2, v3) VALUE (0, 1, 2), (4, 5, 6), etc to add multiple entries faster - but only for one table. Of course the

SELECT LAST_INSERT_ID()

被刻印的时光 ゝ 提交于 2019-11-30 02:29:06
Can somebody explain how works MySQL function LAST_INSERT_ID(). I'm trying to get id of last inserted row in database, but every time get 1. I use mybatis. Example query is : <insert id="insertInto" parameterType="Something" timeout="0"> INSERT INTO something (something) VALUES (#{something}) <selectKey resultType="int"> SELECT LAST_INSERT_ID() </selectKey> </insert> Code: System.out.println("Id : " + id) Output: Id : 1 LAST_INSERT_ID returns the last value implicitly inserted into an AUTO_INCREMENT column in the current session. CREATE TABLE mytable (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY

SELECT LAST_INSERT_ID()

亡梦爱人 提交于 2019-11-28 23:26:51
问题 Can somebody explain how works MySQL function LAST_INSERT_ID(). I'm trying to get id of last inserted row in database, but every time get 1. I use mybatis. Example query is : <insert id="insertInto" parameterType="Something" timeout="0"> INSERT INTO something (something) VALUES (#{something}) <selectKey resultType="int"> SELECT LAST_INSERT_ID() </selectKey> </insert> Code: System.out.println("Id : " + id) Output: Id : 1 回答1: LAST_INSERT_ID returns the last value implicitly inserted into an

Getting last record from mysql

冷暖自知 提交于 2019-11-27 05:22:46
I am using mysql and facing some problem. I want to retrieve last row that is inserted. << Below are details >> Below is how I created table. create table maxID (myID varchar(4)) I inserted four values in it as below insert into maxID values ('A001') insert into maxID values ('A002') insert into maxID values ('A004') insert into maxID values ('A003') When I execute select myID, last_insert_id() as NewID from maxID , I get output as below myId NewID A001 0 A002 0 A004 0 A003 0 When I tried below code, select myId, last_insert_id() as NewID, @rowid:=@rowid+1 as myrow from maxID, (SELECT @rowid:

Get the id of inserted row using C#

泄露秘密 提交于 2019-11-27 01:43:21
I have a query to insert a row into a table, which has a field called ID, which is populated using an AUTO_INCREMENT on the column. I need to get this value for the next bit of functionality, but when I run the following, it always returns 0 even though the actual value is not 0: MySqlCommand comm = connect.CreateCommand(); comm.CommandText = insertInvoice; comm.CommandText += "\'" + invoiceDate.ToString("yyyy:MM:dd hh:mm:ss") + "\', " + bookFee + ", " + adminFee + ", " + totalFee + ", " + customerID + ")"; int id = Convert.ToInt32(comm.ExecuteScalar()); According to my understanding, this

LAST_INSERT_ID() MySQL

断了今生、忘了曾经 提交于 2019-11-26 00:15:31
问题 I have a MySQL question that I think must be quite easy. I need to return the LAST INSERTED ID from table1 when I run the following MySql query: INSERT INTO table1 (title,userid) VALUES (\'test\',1); INSERT INTO table2 (parentid,otherid,userid) VALUES (LAST_INSERT_ID(),4,1); SELECT LAST_INSERT_ID(); As you can understand the current code will just return the LAST INSERT ID of table2 instead of table1, how can I get the id from table1 even if I insert into table2 between? 回答1: You could store