How to compare 2 table with multiple column - mysql

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.

Related

Flushing out table according to results from another table

I have two interconnected tables: one stores some general result (table1), the other one details N rows for each result (table2).
Each record in table2 has a field recording the "auto_id" of the table1 row of reference (field is called "ref_id")
I intentionally deleted some records from the table1, but I left the referenced rows in table2.
How can I find all rows with invalid "ref_id"s in table2 that link to a no more existing "auto_id" in table1?
I was thinking something like
SELECT *
FROM table2
WHERE NOT EXISTS(
SELECT auto_id
FROM 'table1'
WHERE 'table2.res_id' = auto_id
)
but there-s obviously some error.
Thanks for the help!
You are using the wrong quotes. Single-quotes (apostrophes, or ASCII 39) are for literal strings. The easiest solution would be to remove the quotes:
SELECT *
FROM table2
WHERE NOT EXISTS(
SELECT auto_id
FROM table1
WHERE table2.res_id = auto_id
);
If you want to quote identifiers in MySQL, use backticks (ASCII 96).
SELECT *
FROM `table2`
WHERE NOT EXISTS(
SELECT auto_id
FROM `table1`
WHERE `table2`.`res_id` = `auto_id`
);
The cleanest way IMHO is an outer join filtering for missing rows:
SELECT t2.*
FROM table2
LEFT JOIN table1 t1 ON t1.auto_id = t2.res_id
WHERE t1.auto_id IS NULL
This works because missed left joins have all nulls in their columns and where clause conditions are applied after the join is made - the IS NULL condition means the only rows returned are those that don't have a matching row in the other table.
As well as being the most efficient (assuming an index on table1.auto_id), it also makes for a briefer query than a NOT IN (...) query.
MySQL JOIN is the best option for you.
Try this:
SELECT *
FROM table2 T2
JOIN table1 T1
ON T2.`res_id` != T1.`auto_id`

I wanted to know the command to check if all the values in one field of a table is present in another table under a different field name

I have 2 tables. I want to find out whether the values present in the first table is there in another table with a different field name.
Here is how it looks,
Table1
BillNo
43529179
43256787
35425676
25467778
24354758
45754748
Table2
BNo
113104808
25426577
268579679
2542135464
252525232
235263663
I have 137 records in table1 that needs to be checked against table2.
Instead of doing it one by one using the following command,
Select * from Table2 where BNo = '43529179';
This gives the result for just the mentioned value. Is there a way to check for all the values in a single query?
Thanks!
You can use a sub-select to compare against:
Select * from Table2 where BNo IN (SELECT BillNo FROM Table1);
That will "evalaute" to something like Select * from Table2 where BNo IN (113104808, 25426577, 268579679, 2542135464, 252525232, ...);
Join the tables, and check how many matching records there are:
select
count(*) as Matches
from
Table1 as t1
inner join Table2 as t2 on t2.BNo = t1.BillNo
You can also use a left join to pick out the records in table 1 that has no matching record in table 2:
select
t1.BillNo
from
Table1 as t1
left join Table2 as t2 on t2.BNo = t1.BillNo
where
t2.BNo is null

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

select from table based on select distinct from another table

the case is that I need to select a field distinct from table1 (no duplicates) and use the result as a key to select from another table2. And I need this to be in one query. Is this possible?!
table1: hID, hName, hLocation
table2: hID, hFrom, hTo, hRate, hRoomType, hMeals
I want to correct version of this query:
SELECT
*
FROM
table1
JOIN (
DISTINCT
hID
FROM
table2
WHERE
hRoomType = Double Room
ON table1.hID = table2.hID)
expected result: all hotels that offer Double Room thanks much –
thanks for help!
Your question is quite vague and confusing. Is this what you are looking for:
SELECT hID, name, location
FROM table2
INNER JOIN table1
ON table1.hID = table2.hID
GROUP BY table2.hID;
Here is a skeleton to achieve this:
SELECT
* -- Don't forget to list the requested fields instead of using `*`!
FROM (
-- This is the distinct list from table1
SELECT DISTINCT
id
FROM
table1 T1
) DT1
INNER JOIN table2 T2
ON T1.id = T2.reference_to_t1_id
Another solution if you don't want to retrieve any columns from table1:
SELECT
* -- Don't forget to list the requested fields instead of using `*`!
FROM
table2 T2
WHERE
-- Sais that get all record from table2 where this condition matches
-- at least one record
EXISTS (
SELECT 1 FROM table1 T1 WHERE T1.id = T2.reference_to_t1_id
)
For your tables and question
SELECT
hID, hName, hLocation
FROM
table1 T1
WHERE
EXISTS (
SELECT 1 FROM
table2 T2
WHERE
T1.hID = T2.hID
AND T.hRoomType = 'Double' -- Assuming that this is the definition of double rooms
)

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