1

If I'm inserting data into a table with the following fields:

serialNumber active country

I need to only insert duplicate serialNumbers if active is no.

So for example: I want to insert a record with serialNumber 1234.

If the serial number doesn't already exist in the table go ahead and add it. If it does already exist, check the value of 'active' active is yes then don't add the new record, if it's no then do add the record.

Any ideas how to achieve this in MYSQL?

3 Answers 3

2

If the table lacks the necessary unique keys and you do not have permission, or don't want to set the keys you would need, you can use this alternative:

INSERT INTO `table1`
            (`field1`,
             `field2`)
SELECT value1,
       value2
FROM   (SELECT 1) t_1
WHERE  NOT EXISTS (SELECT 1
                   FROM   `table1`
                   WHERE  `field1` = value1
                      AND `field2` = value2);

For yor question it could be written as

INSERT INTO `activity`
            (`serialNumbers`,
             `active`)
SELECT 1234,
       'yes'
FROM   (SELECT 1) t_1
WHERE EXISTS (SELECT 1
              FROM   `activity`
              WHERE  `serialNumbers` = 1234
                AND `active` = 'no');
Sign up to request clarification or add additional context in comments.

Comments

1

You can use the ON DUPLICATE KEY statement after an INSERT INTO query to update the row if it already exists. Documentation : https://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

INSERT INTO table (serialNumber , active, country) VALUES (1010, 'no', 'FR')
  ON DUPLICATE KEY UPDATE active='yes';

5 Comments

This would only seem to work if the serialNumber column is set to unique. Sometimes I do want the serialNumber in there more than once, so long as all the other iterations have the active field set to no. Am I missing something?
What's the primary key of your table ?
I'm sorry - I should have added it - I have an auto incrementing id which is the primary key.
Oh. Take a look a this explanation : techdo.me/… You have to define a unique index, in your case with serialNumber and some other column
Thanks for the answers - however all I can seem to do with this (and it's really useful) is update an existing record when I try and import a duplicate. What I need to do is add a duplicate only if another field is a particular value. I don't want to change what's already in the table, just to add a new record if the criteria are met.
0

You can use the insert ... on duplicate key update in MySQL. It is similar to the MERGE used in other SQL databases, but MySQL does not provide the MERGE statement so this is the next best.

INSERT INTO TABLE (serialNumber, active, country) 
    VALUES (1234, 'active', 'GB') ON DUPLICATE KEY UPDATE country = 'ND';

Also, use INSERT IGNORE if you don't want to generate errors.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.