Multiple inserts/updates without duplication in mysql - 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

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

Related

MYSQL - Multiple value insert into table where not exists in another

I have PHP array(array has multiple values) and i want to add that data in table but before insert into table check for value is exist in another table, if value get in another table skip that value.
Example:
INSERT INTO table1('column1','column2','column3') VALUES
('val11','val11','val11'),
('val12','val12','val12')
WHERE NOT EXIST
(SELECT * FROM table2 WHERE table2.column1 = VALUES(column1) AND
table2.column2 = VALUES(column2) AND table2.column3 = VALUES(column3)
Am I on the right path or is there a better way of doing this?
Thanks
Miten
You need to use INSERT ... SELECT syntax to achieve this result. Because you have multiple rows you have to UNION the data rows into one derived table, and then check that the values in that table don't exist in table2:
INSERT INTO table1 (column1, column2, column3)
SELECT *
FROM (SELECT 'val11' AS column1, 'val12' AS column2, 'val13' AS column3
UNION
SELECT 'val21', 'val22', 'val23') v
WHERE NOT EXISTS (SELECT *
FROM table2 t2
WHERE t2.column1 = v.column1
AND t2.column2 = v.column2
AND t2.column3 = v.column3)
Demo on dbfiddle

How to compare 2 table with multiple column

I am trying to compare multiple column such as part, quantity, and type with another table. I tried with compare part id without problem but when I want to compare part id with quantity it will cause error such as return nothing.
Below coding is for compare part id only.
INSERT INTO `table3`
SELECT *
FROM table1
WHERE NOT EXISTS (SELECT *
FROM table2
WHERE table2.part = table1.part)
How can I compare part and quantity together to find unmatched record.
A handy method in MySQL is in with tuples:
INSERT INTO `table3` ( . . . )
SELECT . . .
FROM table1
WHERE (part, quantity) NOT IN (SELECT part, quantity FROM table2);
More importantly, you should explicitly list all the columns when using an insert. This helps prevent unexpected problems when inserting data.
There are 2 solutions -
1)
INSERT INTO `table3`
SELECT *
FROM table1
WHERE NOT EXISTS (SELECT *
FROM table2
WHERE table2.part = table1.part And table2.quantity=table1.quantity)
2)
INSERT INTO table2 (part, quantity, type)
SELECT part, quantity, type
FROM table1
ON DUPLICATE KEY UPDATE quantity= VALUES(quantity), type= VALUES(type)
Not sure if it's the best approach, but I'd be tempted to try a null-left-join on either a set of values or a concatenation - e.g.
select t1.*
from mytable1 as t1
left join mytable2 as t2 on t1.foo = t2.foo and t1.bar = t2.bar
where t1.id is null;
The only thing to note is that t1.id (and t2.id) are assumed to be the primary key - i.e. can never be null. The end result of the above should be all the rows in t1 that cannot be joined to a row in t2.

Insert into Table2 only if values being inserted are found in Table1

I have two tables Table1 and Table2.
I use an insert on Table2 like below:
insert into table2
(colOne, colTwo, colThree) //and many more cols
values
(1, norman, US) //and many more values that come from a form and not table1
I'd like the insert to succeed only if values (1,norman, US) are present in Table1. Values (1,Norman,US) come from a form. How can I do something like this.
At present I used 2 steps to do this. One to check if the values exist, two - the insert
You may use an INSERT INTO... SELECT... WHERE
Something liket that
insert into table2 (col1, col2, col3)
select (1, 'norman', 'US') from Table1 t1 -- where 1, 'norman', 'US' are your variables from a Form
where t1.id=1 and t1.name = 'norman' and t1.country = 'US' -- same
little SqlFiddle demo for "select whatever I want".
You can use this method :
INSERT INTO table2
(colOne, colTwo, colThree)
SELECT colOne, colTwo, colThree
FROM
(SELECT 1 AS colOne,'norman' AS colTwo,'US' AS colThree
UNION
SELECT 2,'sabt','US'
UNION
SELECT 3,'ebi','US'
)p
WHERE
EXISTS (
SELECT 1 FROM table1
WHERE table1.colOne = p.colOne AND
table1.colTwo =p.colTwo AND
table1.colThree =p.colThree
)
Good luck.
Something like this is normally done with triggers. Register an after insert trigger for table1 and do an insert for table2.
http://technet.microsoft.com/de-de/library/ms189799.aspx

Copy rows from one table to another, ignoring duplicates

Im trying to copy row from table to another using 2 coluom only as the tow table schema is not identical ,
am getting this error
Operand should contain 1 column(s)
Any tips whats wrong with my statement ?
Insert table1 ( screenname,list_id )
Select screenname,list_id
From table2 As T1
Where Not Exists (
Select 1
From table1 As T2
Where
(T2.screenname = T1.screenname,T2.list_id = T1.list_id)
)
try to change where condition from (T2.screenname = T1.screenname,T2.list_id = T1.list_id) to (T2.screenname = T1.screenname AND T2.list_id = T1.list_id)
(note AND keyword instead of comma)
Did you try INSERT INTO...ON DUPLICATE KEY syntax?
See MySQL manual here
You can create a unique index in table1 on the columns screenname and list_id
Then use the following statement
Insert ignore into table1 ( screenname,list_id )
Select screenname,list_id
From table2 As T1
Also try this query -
INSERT INTO table1 (screenname, list_id)
SELECT screenname, list_id FROM table2 t2
LEFT JOIN table1 t1
ON t1.screenname = t2.screenname AND t1.list_id = t2.list_id
WHERE
t1.screenname IS NULL AND t1.list_id IS NULL;
Use simple INSERT IGNORE
INSERT table1 (screenname, list_id) SELECT screenname, list_id FROM table2

MYSQL, Copy selected fields from one table to another

In MySQL, How do I copy a FIELD with all RECORDS from TABLE1 to TABLE2 which corresponds to a primary key ie: EMPLOYEE no.?
If you mean you want to update one table's column using another table's column, then here are some options:
A join:
UPDATE table1 AS t1
INNER JOIN table2 AS t2 ON t1.EmpoyeeNo = t2.EmployeeNo
SET t1.SomeColumn = t2.SomeColumn
Alternatively it could be a left join:
UPDATE table1 AS t1
LEFT JOIN table2 AS t2 ON t1.EmpoyeeNo = t2.EmployeeNo
SET t1.SomeColumn = t2.SomeColumn
which would essentially empty (set to NULL) the values where no match occurred.
A subquery:
UPDATE table1
SET SomeColumn = (
SELECT SomeColumn
FROM table2
WHERE EmployeeNo = table1.EmployeeNo
)
This is equivalent to the left join solution in #1.
Note that in all cases it is assumed that a row in table1 can match no more than one row in table2.
Try this
INSERT INTO `table2` (`field_name2`) SELECT `field_name` FROM `table1`
The query for copy data from one table to another is:
INSERT INTO `table2` (`field1`, `field2`)
SELECT `field1`, `field2` FROM `table1`
If you want to copy only selected values, then use where clause in query
INSERT INTO `table2` (`field1`, `field2`)
SELECT `field1`, `field2` FROM `table1`
WHERE `field1` = condition
update
table1 t1
join table2 t2 on t2.field = t1.field
set
t1.field1 = t2.matchingfield
where
t1.whatever = t2.whatever
You can use this to copy all the records from table1 into table2 with a condition.
Insert into table2 select * from table1 where field1=condition
Suppose if the table structure is as follows.
TableA - Col1, Col2 ,Col3
TableB - Col1, Col2 ,Col3
There is no need to select all column of the table to transfer data from 1 table to another table in same databse.
You can copy (insert) the rows from TableA to TableB.
Code as follows -
Insert into TableB (Col1, Col2 ,Col3)
Select Col1, Col2 ,Col3 from TableA
You can also do this -
Insert into TableB (Col1, Col2, Col3)
Select * from TableA
Both codes work , you need to see your requirement.
Generic code -
INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM table1
WHERE condition;
You can add 'Where' condition if you need.
Thank you!!!
INSERT INTO table_1(column-1, column-2) SELECT column-1, column-2 FROM table_2;
Insert into Delivery (DeliveredDate, appid, DownloadSize, UploadSize) select Delivered, Appid, DownloadSize,UploadSize from Delivery_Summary;