Say, I've got a simple table: id
as primary key, and field
. Then i do:
INSERT INTO table(id,field) VALUES (1,'blah')
and then I do it again. So, how can I make MySQL return following:
id
1
So that it works like SELECT-ing conflicted key?
Say, I've got a simple table: id
as primary key, and field
. Then i do:
INSERT INTO table(id,field) VALUES (1,'blah')
and then I do it again. So, how can I make MySQL return following:
id
1
So that it works like SELECT-ing conflicted key?
I don't think that's possible without some code, mysql will return a generic duplicate key error
With-Code solution:
Are you using an AUTO_INCREMENT column ?
If you use INSERT IGNORE and the row is ignored,
the AUTO_INCREMENT counter is not incremented and LAST_INSERT_ID() returns 0,
which reflects that no row was inserted.
so:
You might consider the following...
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table(my_id INT NOT NULL PRIMARY KEY,my_field VARCHAR(12) NOT NULL,conflict TINYINT NOT NULL DEFAULT 0);
INSERT INTO my_table (my_id,my_field) VALUES (1,'blah') ON DUPLICATE KEY UPDATE conflict = 1;
SELECT * FROM my_table WHERE conflict = 1;
Empty set (0.00 sec)
INSERT INTO my_table (my_id,my_field) VALUES (1,'blah') ON DUPLICATE KEY UPDATE conflict = 1;
Query OK, 2 rows affected (0.07 sec)
SELECT * FROM my_table WHERE conflict = 1;
+-------+----------+----------+
| my_id | my_field | conflict |
+-------+----------+----------+
| 1 | blah | 1 |
+-------+----------+----------+