Jhanvi
Jhanvi

Reputation: 5139

Multiple inserts/updates without duplication in mysql

I want many values to be simultaneously inserted in my table having only 2 columns and if those values already exists then it has to be updated.. Though duplication for 1 column is possible but not for the second column.. I can easily do it with the following query.. But the problem is here only one row can only be considered... There are no primary keys.. PLZ HELP

INSERT INTO `table` (value1, value2) 
SELECT 'stuff for value1', 'stuff for value2' FROM `table` 
WHERE NOT EXISTS (SELECT * FROM `table` 
                  WHERE value1='stuff for value1' AND value2='stuff for value2') 
LIMIT 1

Upvotes: 0

Views: 92

Answers (1)

Sashi Kant
Sashi Kant

Reputation: 13465

Try this Insert into table name............. on duplicate key update set column1=......

Alternative way ::

Step1 : Create a temp_table with same structure of that of table1

Step 2:

    INsert into temp_Table 
   (SELECT * from table1 t1 left join table2 t2 on (t1.value1=t2.value1 and t1.value2=t2.value2)
    where t2.value1 is null and t2.value2 is null);

Step3:

  INsert into table Select * from temp_table 

Upvotes: 2

Related Questions