Here's my database schema
+------------------+------------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+------------------+------+-----+---------------------+----------------+
| phone_number | varchar(64) | NO | UNI | NULL | |
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
+------------------+------------------+------+-----+---------------------+----------------+
I'd like to be able to insert several phone numbers (phone_number is a unique key) at once, but I don't want to increment the auto_increment field if I have duplicates.
If I do
INSERT INTO
phone_numbers (phone_number)
VALUES
(
%
VALUES
%
)
ON DUPLICATE KEY
UPDATE
id = id;
the auto_increment will increase even for duplicates.
This question: Prevent auto increment on MySQL duplicate insert doesn't handle bulk inserts. I'd like to do something like this:
INSERT INTO
phone_numbers (phone_number)
SELECT
'12345',
'123456'
FROM
DUAL
WHERE
NOT EXISTS
(
SELECT
phone_number
FROM
phone_numbers
WHERE
phone_number IN
(
'12345',
'123456'
);
but the DUAL
table doesn't really handle multiple values well.
Any ideas? MySQL 5.5.