Linked Questions
20 questions linked to/from MySQL ON DUPLICATE KEY - last insert id?
1185 votes
13 answers
1.4m views
Insert into a MySQL table or update if exists
I want to add a row to a database table, but if a row exists with the same unique key I want to update the row. For example: INSERT INTO table_name (ID, NAME, AGE) VALUES(1, "A", 19); Let’s ...
40 votes
4 answers
40k views
Create if an entry if it doesn't exist, otherwise update?
Kinda strange to put it into words that short, heh. Anyway, what I want is basically to update an entry in a table if it does exist, otherwise to create a new one filling it with the same data. I ...
28 votes
3 answers
28k views
mysql - after insert ignore get primary key
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 ...
16 votes
3 answers
12k views
"Insert ignore" vs "select and insert"
I want to write a program add new item to table. This item has an unique key name and it can be created by one of 100 threads, so I need to make sure that it is inserted only once. I have two ideas: ...
15 votes
2 answers
11k views
INSERT and return ID, or if DUPLICATE KEY return existing ID in MySQL
I have a "tags" table, which has a UNIQUE on the "tags" column, to prevent duplicates. I have a "bookmarks_tags" table, which relates bookmarks to tags, and it has a UNIQUE on the "bookmark_id" and "...
9 votes
5 answers
14k views
How to get ID row of updated row?
This is my query: INSERT INTO table (value) VALUES (value) ON DUPLICATE KEY UPDATE value=value With mysql_insert_id() I get the new inserted ID, but how to get the ID of row updated? I tried ...
3 votes
2 answers
9k views
INSERT ON DUPLICATE KEY UPDATE with last_insert_id()
im trying to create a function CREATE FUNCTION `func`(param1 INT, param2 INT, param3 TEXT) RETURNS int(11) BEGIN INSERT INTO `table1` (`column1`, `column2`, `column3` ) VALUES (param1, param2, ...
2 votes
2 answers
4k views
Getting last insert id for mysql after Insert
I have a query like so: INSERT INTO table1 (field1,field2) VALUES ('$value1','$value2') ON DUPLICATE KEY UPDATE field1 = '$value1' I then want to get the last insert id if it does the insert, how can ...
1 vote
1 answer
4k views
How to get lastrowid from sqlalchemy after INSERT .. ON DUPLICATE KEY UPDATE?
I'm using sqlalchemy to do an INSERT ... ON DUPLICATE KEY UPDATE in MySQL, and I'm trying to find the lastrowid of the primary key that was inserted or updated. However, when the DUPLICATE clause is ...
4 votes
2 answers
2k views
MySQL INSERT only if row doesn't exist, otherwise select the duplicate row
I am looking for a statement that will try to insert a row into a table, but return the primary key of the duplicate row if one is encountered. One field in the table is the auto incrementing primary ...
0 votes
1 answer
1k views
PHP script keeps timing out while importing data from CSV to MySQL
I'm stumped on this one. I'm a little above beginner-level with PHP/MySQL and very new with posting on this site. GoDaddy switched me over to a grid server to boost performance and shed light on a ...
0 votes
1 answer
979 views
mysql insert ... on duplicate key update return inserted row id
OK the question is NOT about how to return the ID, but more like how to skip it... I have a situation where i use last_insert_id() to get the id of the last inserted row, but i want to get a zero if ...
0 votes
1 answer
643 views
MySQL INSERT with ON DUPLICATE SELECT possible?
email_address ------------- id prefix fqdn Prefix and fqdn together form a unique key. I would like to write a query which would do an insert, if the email address is not yet in the table. If the ...
0 votes
1 answer
426 views
MySQL Insert where not exists does not work
Pasted below is my MySQL Stored Procedure. CREATE PROCEDURE `newrig`( IN x varchar(10), IN y varchar (10), IN z varchar(5), OUT a INT ) BEGIN INSERT INTO rig (Name, Model,Type) ...
0 votes
2 answers
292 views
Get last ID of a newly inserted element, or of an existing one
I have a following map table, with a unique key on the name column: id | name 1 aaa 2 bbb 3 ccc I want to get a newly created ID if I insert a new value to this table, like this: INSERT ...