Select between two tables, two columns - mysql

I am trying to find all records in TableA which are not in TableB by comparing 2x columns in each table. I have tried all sorts of queries but I cannot figure it. Any help would be much appreciated.
TableA Has ColumnA and ColumnDate
TableB Has ColumnA and ColumnDate
So I only want to see which records in TableA do not match TableB for both columns.
Also, TableA may have several matching fields so I need to group them to have only 1 entry into TableB for each match.
Yes I want to insert the new records into TableB.
It appears harder than expected.
Something like this:
select ColumnDate, ColumnA
from TableA
where
(( ColumnDate not in (select ColumnDate from TableB) )
and ( ColumnA not in (select ColumnA from TableB) ))
group by ColumnA, ColumnDate;

There are two approaches to this, If I want to just sync two tables I use straight INSERT SELECT.
INSERT INTO TableB (ColumnA, ColumnDate)
SELECT FROM TableA (ColumnA, ColumnDate)
ON DUPLICATE KEY UPDATE ColumnDate = ColumnDate
But be careful, you need to have Primary Key or Unique index on TableB, so the engine will only insert new rows based on uniqueness/pk of the receiving table.

Related

Can you link a column in one table which has column names of another table with that second table

I have two tables A and B
Table A has a list of all columns in table B
Table B has values in each column
In MS Access, is it possible to create a query linking the values in the column in Table A with the Columns in Table B?
The SELECT statement I am thinking of is something along the lines of:
SELECT A.Col1,A.Col2,B.Col1,B.Col2
FROM TableA AS A
LEFT OUTER JOIN TableB AS B
-- this is where I get stuck but in pseudo-code might be ..
ON value in A.Col3 = B.Col3
Thanks for any help on this even if it's "No, this can't be done in Access"
TableB is not normalized data structure. It has no relationship with TableA and join is not possible nor does it make sense. Do a UNION query on TableB to reorganize data to normalized structure.
I presume TableB has a unique identifier field.
SELECT ID, "SourceOfReferrals_SQ001" AS Source, SourceOfReferrals_SQ001 AS Data FROM TableB
UNION SELECT ID, "SourceOfReferrals_SQ002", SourceOfReferrals_SQ002 FROM TableB
UNION SELECT ID, "SourceOfReferrals_SQ003", SourceOfReferrals_SQ003 FROM TableB
UNION SELECT ID, "SourceOfReferrals_SQ004", SourceOfReferrals_SQ004 FROM TableB
UNION SELECT ID, "SourceOfReferrals_SQ005", SourceOfReferrals_SQ005 FROM TableB;
Now this query can be joined to TableA if there are other fields in TableA you want data from.
Alternative is VBA looping fields of recordset to write data to a normalized table.

Select columns without using subquery in mysql

I want to select some records from table "A" but records that are not in table "B".
Example...
Tables are...
A{A_ID, A_Date, A_Price};
B{B_ID, A_ID};
I want to select records from table "A" with primary key A_ID but only those records that are not table "B" on joining both table on primary key A_ID.
I can do this with query...
select * from A where A_ID not in (select A_ID from B)
but my problem is subquery. Because it takes too much time run, if data quantity big.
SO I WANT TO RUN IT WITHOUT SUBQUERY.
please help!!!
Try these queries:
select * from TableA A
where not exists(select 1 from TableB where A_ID = A.A_ID)
or
select A.* from TableA A left join TableB B
on A.A_ID = B.A_ID
where B.B_ID is null

SQL Query to find missing records between two tables and then update the second with the missing records from the first

Two tables. 8 fields in each. Both tables have the same data, one with
137,002 record (tablea) and one with 135,759 records (tableb). Both tables share a common primary field if three columns (qid, sid, aid).
Is there a single query that will.
1) compare tablea to tableb on the primary field
and
2) if the record is in tablea and not tableb copy the record from tablea to tableb
I would rather be able to update tableb with an sql query rather than writing a php loop to go through the 137,002 and do a compare on each one.
Thanks
That should be smth looking like:
insert into table2 (qid, sid ...)
select
t1.qid,
t1.sid,
...
from table1 t1
where
not exist (select t2.qid, t2.sid, ... from table2 t2 where t2.qid = t1.qid and t2.sid = t1.sid...)
INSERT INTO tableb AS b
(SELECT * FROM tablea AS a WHERE NOT EXISTS (SELECT * FROM tableb AS b2 WHERE b2.id = a.id))
Use merge...and use insert only....not update.
So, the following worked.
insert into f_step_ans (`qid`, `assid`, `sid`, `sas`, `cas`, `etim`, `stim`, `endtim`, `fil`)
select
t1.qid,
t1.assid,
t1.sid,
t1.sas,
t1.cas,
t1.etim,
t1.stim,
t1.endtim,
t1.fil
from f_step_ans_back t1
where
not exists (select t2.qid, t2.sid,t2.assid from f_step_ans as t2 where t2.qid = t1.qid and t2.assid = t1.assid and t2.sid = t1.sid)
1,588 records were moved from the f_step_ans_back table (old backup table) to the f_step_ans table (partially recovered backup + new data). Reporting shows that everything is working like it should be. Thank you all for the help.

Insert missing records from one table to another using mysql

I don't know why I am confused with this query.
I have two table: Table A with 900 records and Table B with 800 records. Both table need to contain the same data but there is some mismatch.
I need to write a mysql query to insert missing 100 records from Table A to Table B.
In the end, both Table A and Table B should be identical.
I do not want to truncate all the entries first and then do a insert from another table. So please any help is appreciated.
Thank you.
It is also possible to use LEFT OUTER JOIN for that. This will avoid subquery overhead (when system might execute subquery one time for each record of outer query) like in John Woo's answer, and will avoid doing unnecessary work overwriting already existing 800 records like in user2340435's one:
INSERT INTO b
SELECT a.* FROM a
LEFT OUTER JOIN b ON b.id = a.id
WHERE b.id IS NULL;
This will first select all rows from A and B tables including all columns from both tables, but for rows which exist in A and don't exist in B all columns for B table will be NULL.
Then it filter only such latter rows (WHERE b.id IS NULL),
and at last it inserts all these rows into B table.
I think you can use IN for this. (this is a simpliplification of your query)
INSERT INTO table2 (id, name)
SELECT id, name
FROM table1
WHERE (id,name) NOT IN
(SELECT id, name
FROM table2);
SQLFiddle Demo
AS you can see on the demonstration, table2 has only 1 records but after executing the query, 2 records were inserted on table2.
If it's mysql and the tables are identical, then this should work:
REPLACE INTO table1 SELECT * FROM table2;
This will insert the missing records into Table1
INSERT INTO Table2
(Col1, Col2....)
(
SELECT Col1, Col2,... FROM Table1
EXCEPT
SELECT Col1, Col2,... FROM Table2
)
You can then run an update query to match the records that differ.
UPDATE Table2
SET
Col1= T1.Col1,
Col2= T1.Col2,
FROM
Table T1
INNER JOIN
Table2 T2
ON
T1.Col1 = T2.Col1
Code also works when a group by and having clauses are used. Tested SQL 2012 (11.0.5058) Tab1 is source with new records, Tab 2 is the destination to be updated. Tab 2 also has an Identity column. (Yes folks, real world is not as neat and clean as the lab assignments)
INSERT INTO Tab2
SELECT a.T1,a.T2,a.T3,a.T4,a.Val1,a.Val2,a.Val3,a.Val4,-9,-9,-9,-9,MIN(hits) MinHit,MAX(hits) MaxHit,SUM(count) SumCnt, count(distinct(week)) WkCnt
FROM Tab1 a
LEFT OUTER JOIN Tab2 b ON b.t1 = a.t1 and b.t2 = a.t2 and b.t3 = a.t3 and b.t4 = a.t4 and b.val1 = a.val1 and b.val2 = a.val2 and b.val3 = a.val3 and b.val4 = a.val4
WHERE b.t1 IS NULL or b.Val1 is NULL
group by a.T1,a.T2,a.T3,a.T4,a.Val1,a.Val2,a.Val3,a.Val4 having MAX(returns)<4 and COUNT(distinct(week))>2 ;

A way to SELECT fields from one table and INSERT to another?

I have two tables, TableA and TableB
TableA has 9 fields
TableB has 7 fields
There are 2 fields (id and name) that are identical in both tables, is there a way to select ONLY these two fields from TableA and insert them into TableB?
I have looked at the INSERT INTO... SELECT method using this statement:
INSERT INTO TableB
SELECT id, name
FROM TableA
WHERE id = 1
But I get the following error:
#1136 - Column count doesn't match value count at row 1
I assume this error is not allowing me to insert only 2 fields into the table? If so, is there a way around this or an alternative method?
Thanks
Try:
INSERT INTO TableB(id, name)
SELECT id, name FROM TableA where id = 1;
One would have to assume that the column names in TableB match TableA otherwise you would need to put in the right names.
You need to specify the column names for TableB (and possibly specify TableA.id in the WHERE clause):
INSERT INTO TableB (id, name)
SELECT (id, name)
FROM TableA
WHERE TableA.id = 1
Specify the columns in table b
INSERT INTO TableB (id, name)
SELECT id, name
FROM TableA
WHERE id = 1