How to transfer data between tables, only where id does not exist - mysql

I need to copy a set of data from TableA into TableB, like so:
INSERT INTO TableB(id,field1,field2)
SELECT id,field1,field2 FROM TableA
The above will work well, however TableB might already contain some of the records which I need to copy, identified by the PK id.
Thus, how can I add a clause to only insert a record if that id value is not already in TableB? I know a WHERE clause can be added at the end of the INSERT statement, but I am unsure of how to apply it for each and every record.

You can take a look for 3 methods comparing NOT IN vs. NOT EXISTS vs. LEFT JOIN / IS NULL.
The best way to search for missing values in MySQL is using a LEFT
JOIN / IS NULL or NOT IN rather than NOT EXISTS.
You can use NOT EXISTS.
INSERT INTO TableB (id, field1, field2)
SELECT id, field1, field2
FROM TableA t1
WHERE NOT EXISTS (
SELECT *
FROM TableB t2
WHERE t1.id = t2.id
)
Also you can use LEFT JOIN.
INSERT INTO TableB (id, field1, field2)
SELECT id, field1, field2
FROM TableA t1
LEFT JOIN TableB t2 ON t1.id = t2.id
WHERE t2.id IS NULL
Also you can use NOT IN.
INSERT INTO TableB (id, field1, field2)
SELECT id, field1, field2
FROM TableA t1
WHERE t1.id NOT IN (
SELECT t2.id
FROM TableB t2
WHERE t1.id = t2.id
)

You can use left join as below
INSERT INTO TableB(id,field1,field2)
SELECT id,field1,field2 FROM TableA
left join TableB on TableB.id = TableA.id
where TableB.id is null

you might want a where clause with exists()
INSERT INTO TableB(id,field1,field2)
SELECT id,field1,field2 FROM TableA a
WHERE not exists (SELECT 1 FROM TableB b WHERE a.id = b.id)

INSERT INTO TableB(id,field1,field2)
SELECT A.id,A.field1,A.field2 FROM TableA WHERE NOT EXISTS
(SELECT B.ID FROM TABLEB WHERE B.ID = A.ID)

Try to use a left join:
INSERT INTO TableB(id,field1,field2)
SELECT id,field1,field2 FROM TableA left join TableB on tableA.id = tableb.id where tablea.id is null

Better to use set operator EXCEPT for best performance as below:
here it will take two sets of data the it goes for minus and gives output
INSERT INTO TableB(id,field1,field2) (
SELECT id,field1,field2 FROM TableA
except
SELECT id,field1,field2 FROM TableB )

INSERT INTO TableB(id,field1,field2)
SELECT T2.id,T2.field1,T2.field2 FROM TableA as T1
inner join TableB as T2 on T1.id <> T2.id

Related

how to return all the fields of table2 based upon the occurrence of the id in the table1

I have 2 tables, one is table1
and another is table 2
I want the result by a query, like
I have tried select id from table2 order by (select id from table1); but it is giving error.
You can join and sort. But you need a column that defines the ordering of the rows in table1. Let me assume that you have such column, and that is is called ordering_id.
select t2.*
from table2 t2
inner join table1 t1 on t1.id = t2.id
order by t1.ordering_id
You can even use a subquery in the order by clause:
select *
from table2 t2
order by (select t1.ordering_id from table1 t1 where t1.id = t2.id)
Join the two tables and then order the result.But for that you need to have some column for ordering and this does not seems to be the case. Syntax you are using for ordering will not work.
SELECT A.ID, B.NAME FROM TABLE1 A INNER JOIN TABLE2 B
ON(A.ID = B.ID) ORDER BY A.ID DESC
finally got the answer
select t2.*
from table2 t2
inner join table1 t1 on t1.id = t2.id;

SQL query to find unreferenced records

I have two tables bound through an ID field:
table1: id, name, type
table2: id, id_table1, date, status
I have to collect all the records of the table1 that have a certain value of type field and that are not been referenced in table2 plus all the records of table1 referenced in table2 that have a certain status field value.
For the first part if I remember correctly I can use the LEFT JOIN command:
LEFT JOIN table1.name
LEFT JOIN table2
ON table2.id_table1 = table1.id
WHERE (table1.value = 'value1') AND (table2.id_table1 IS NULL);
but for the second part I'm getting lost...
I'm using MySQL 5.6 and I would like to define a View to handle this.
SELECT t1.*, t2.*
FROM table1 t1
LEFT JOIN table2 t2
ON table2.id_table1 = table1.id
WHERE (t1.type= 'value1' AND t2.id IS NULL)
OR (t2.status = 'certain status' )
I would think you could just change the WHERE to:
WHERE (table1.value = 'value1')
AND (table2.id_table1 IS NULL
OR
([the other table2 status criteria)
)
;
You can try this...
SELECT T1.*,T2.*
FROM Table1 T1
LEFT JOIN Table2 T2 ON T1.Id=T2.Id_Table1
WHERE T1.Value = 'value1' AND T2.id_table1 IS NULL
UNION
SELECT T1.*,T2.*
FROM Table1 T1
INNER JOIN Table2 T2 ON T1.Id=T2.Id_Table1
WHERE T2.Status= 'Status Criteria'

Mysql Update table1 from table2 values with CASE

I have table A & Table B . I need to update TableA.custid , with values (1 - 8 ) so I can reflect TableB.names
To make it more clear
TableA.custid = 1 , when TableB.name = 'Allen Gray'
TableA.custid = 2 , when TableB.name = 'Alex Watt'
TableA and TableB got the same number of records. And the relation between the 2 would be TableA.id = TableB.id
What would be the syntax for this?
Update tableA as t1 inner join TableB as t2
on t1.id=t2.id
set t1.name=t2.name

Inserting records into Table A from Table B only if they are not avaiable in Table A

I have two tables.. Table A and Table B (MySQL)
Table-A has id,value,name.. and
Table-B has id,name,email,address..
I like to insert 'id' from Table B as 'Value' in Table A only if that 'id'(TableB) does not exist as'Value' in TableA
So can i use this
INSERT INTO `TableA`( `value`, `name`)
SELECT id,name FROM TableB
WHERE TableB.id NOT IN TableA.value
You need to use code along the lines of the following:
WHERE NOT EXISTS (select * from TableA where TableA.value = TableB.id)
You can use this solution:
INSERT INTO TableA (value, name)
SELECT b.id, b.name
FROM TableB b
LEFT JOIN TableA a ON b.id = a.value
WHERE a.id IS NULL

inner join for a query?

I want to do a sql query and have some problems:
I want to select from table_1 the ID's Where parent_id is the value I have:
SELECT ID
FROM table_1
WHERE parent_ID = 'x'
I want to use the ID'S I got in 1. and
SELECT
FROM table_2
WHERE ID = 'The ID's from Query 1.'
Like this?
select ...
from table_1 a
join table_2 b on(a.id = b.id)
where a.parent_id = 'x';
Edit
Note: the query will potentially produce duplicate rows depending on the keys and relation between the tables. For example, you will get duplicates if, for a given table_1.parent_id = X, there can be multiple occurrences of the same table_1.ID.
Another example is when table_2.ID isn't unique.
In those cases you would want to remove the duplicates (using distinct, group by, partitioned #row_number, etc) or, not produce the duplicates in the first place using a semi-join instead (exists, in). Have a look #OMG Ponies answer for reference.
Using IN
SELECT t2.*
FROM TABLE_2 t2
WHERE t2.id IN (SELECT t1.id
FROM TABLE_1 t1
WHERE t1.parent_id = 'x')
Using EXISTS
SELECT t2.*
FROM TABLE_2 t2
WHERE EXISTS (SELECT NULL
FROM TABLE_1 t1
WHERE t1.id = t2.id
AND t1.parent_id = 'x')
Using an INNER JOIN
The DISTINCT (or GROUP BY) is necessary to eliminate duplicates if there are more than one records in TABLE_1 that relate to a record in TABLE_2:
SELECT DISTINCT t2.*
FROM TABLE_2 t2
JOIN TABLE_1 t1 ON t1.id = t2.id
AND t1.parent_id = 'x'
It can be solved with the use of IN as follows:
SELECT * FROM table_2 WHERE ID IN (SELECT ID FROM table_1 WHERE parent_ID = 'x')
select * from table_2 where id in (select id from table_1 where parent_id = 'x')
Yes, it's better to you use this:
SELECT [value]
FROM [table2]
WHERE [value] IN (SELECT [value]
FROM [table1]
WHERE [value] = "[value]"
)