Copy rows from one table to another, ignoring duplicates - mysql

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

Related

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.

Update/Insert column values with another table's group by result in mysql

I have an empty table (t1) and I want to insert or update the t1.uid column from another table's (t2) GROUP BY uid values.
So far I have tried like this:
UPDATE table1 t1 JOIN
(SELECT uid FROM table2 GROUP BY uid) t2
SET t1.uid = t2.uid;
but it's not working for me.
N.B. I've got a massive data set for which group by (uid from table-t2) results giving me total 1114732 results which I have to insert/update in t1 table's uid column.
Please try this:
Insert into table1(uid)
select distinct uid from table2
If table1 is empty, then UPDATE is not the correct verb. Would this suit your needs?
INSERT into table1 SELECT distinct uid from table2;
INSERT ... SELECT docs

SQL Select on multiple tables to check if value exists in one table and not used in other

I have 2 tables:
table1 (id,usedcode)
table2 (codeid,uniquecode)
I want to be able to check if a certain value exists in uniquecode of Table2, but is not already used in Table1
Try using left join as below:
SELECT t2.*
FROM table2 t2 LEFT JOIN table1 t1
ON t2.uniquecode = t1.usedcode
WHERE t1.usedcode IS null
SELECT uniquecode FROM Table2
WHERE NOT EXISTS(
SELECT * FROM Table1 WHERE usedcode = uniquecode
)
In English the query is saying, "Select all unique codes from table 2 that don't exist in table 1 as a usedcode".

SQL: insert only new records

This must be very trivial but I can't seem to find the solution.
I work with two tables, both without any primary key.
I want to add all the records of the first table to the second table only if they don't exist.
Basically:
INSERT INTO Table2
SELECT Table1.*
FROM Table
WHERE "the record to be added doesn't already exists in Table2"
You could do something like this. You would need to check each relevant column - I have just put in 2 as an example. With a Not Exists clause you can check if a record already existed across multiple columns. With a NOT IN you would only be able to check if a record already existed against one column.
INSERT INTO Table2
SELECT t1.*
FROM Table1 t1
WHERE NOT EXISTS
(
SELECT 1
FROM table2 t2 WHERE
t2.col1 = t1.col1 AND
t2.col2 = t1.col2
)
you could make usage of the EXISTS function:
INSERT INTO Table2
SELECT Table1.*
FROM Table1
WHERE NOT EXISTS(SELECT * FROM table2 WHERE <your expression to compare the two tables goes here>)
But i would advise you to check the use of unique index for your tables
Just an idea - untested:
INSERT INTO Table2
SELECT *
FROM Table1
WHERE NOT EXISTS(SELECT * FROM Table2 WHERE Table2.Field1 = Table1.Field1 AND Table2.Field2 = Table1.Field2)
You must add every Field of both Tables in the WHERE clause of the NOT EXISTS Query
INSERT INTO X.TableX1
(ColumX1,ColumnX2)
SELECT DISTINCT c.[ColumnY1],c.[ColumnY2]
FROM Y.Table2 c INNER JOIN Database.z.Table3 i ON c.ColumnX1= i.ColumnY1
WHERE NOT EXISTS
(
SELECT *
FROM X.TableX1 WHERE
X.TableX1.ColumnX1=c.ColumnY1
)
This is Joining two tables and filtering the data required and updating only the new values to third table on every run

Best way of deleting SQL rows how i want

How can i construct a SQL query to delete how i want.
I have two tables.
Table 1.
ID: Some Random Not Significant To This Question Columns : DateTime : UserID
Table 2.
ID: Some Random Not Significant To This Question Columns : DateTime : UserID
The two tables are related by DateTime and UserID
Is there anyway i can create a query so that it deletes from table 2 if no rows in table1 have a matching DateTime & UserID.
Thanks
You can use LEFT JOIN :
DELETE table2
FROM table2 t2 LEFT JOIN table1 t1 ON t1.`DateTime` = t2.`DateTime`
AND t1.`UserID` = t2.`UserID`
WHERE t1.`UserID` IS NULL
DELETE
FROM table2 t2
WHERE NOT EXISTS
(
SELECT NULL
FROM table1 t1
WHERE (t1.userId, t1.dateTime) = (t2.userId, t2.dateTime)
)
First of all: create a backup before you delete lots of records :)
The idea:
DELETE FROM
table1
WHERE
NOT EXISTS (SELECT 1 FROM table2 WHERE table1.referenceColumn = table2.referenceColumn)
You can check which records will be deleted by replacing the DELETE with SELECT *
And now the solution
DELETE FROM
table2
WHERE
NOT EXISTS (
SELECT 1 FROM
table1
WHERE
table2.UserID = table1.UserID
AND table2.DateTime = table1.DateTime
)