Find values that fail sql join on - mysql

Say I have a query such as:
SELECT *
FROM Table_1
JOIN Table_2
ON Table_1.Col_1 = Table_2.Col_1
So I have 100 records and 98 of them are equal so that query would print out 98 out of 100. How can I get SQL to print the 2 that failed to meet the join?

Use a LEFT JOIN:
SELECT *
FROM Table_1
LEFT JOIN Table_2 ON (Table_1.Col_1 = Table_2.Col_1)
The fields of Table_2 will be NULL where there was no match for the ON clause. You'll then be able to add a WHERE TABLE_2.Col_1 IS NULL to keep only records in Table_1 that didn't have a match in Table_2.

An alternative to the LEFT JOIN is to use EXISTS.
SELECT * FROM Table_1
WHERE NOT EXISTS (SELECT * FROM Table_2 WHERE Col_1 = Table_1.Col_1)

try this:
SELECT * FROM `Table_1` LEFT JOIN `Table_2` ON (`Table_1`.`Col_1` = `Table_2`.`Col_1`)

SELECT *
FROM Table1 AS A
LEFT OUTER JOIN Table2 AS B
ON (A.Col = B.Col)
WHERE B.Col IS NULL

Related

MySql - select from main table with a lot of connected tables

I have a main table, which is related to 10 other tables through one-to-many relationships.
For example:
main Table table1 table2
---------- ------- -------
id * id* id*
name mainTableId ** mainTableId **
column1 column2
I have tried:
SELECT *
FROM main_table
LEFT OUTER JOIN table1 On table1.mainTableId = main_table.id
LEFT OUTER JOIN table2 On table2.mainTableId = main_table.id
...
The problem is that I am getting a lot of results back, since each result in main_table is being multiplied by all the results in table1, table2, etc.
I would like to use only one SELECT query, since querying the database 10 times takes a long time.
Retrieving 100 results from each table is good enough for my needs, but trying to do the following resulted in an error that limit is not supported with a sub query:
LEFT OUTER JOIN table1 WHERE id in (
SELECT id FROM table1 WHERE mainTableId = mainTable.id LIMIT 100
)
I am using AWS Aurora. How can I improve this query?
SELECT *
FROM main_table
LEFT OUTER JOIN table1 ON main_table.id = table1.mainTableId
LEFT OUTER JOIN table2 ON main_table.id = table2.mainTableId
...
For the second question you could just fetch 100 rows from the other tables and join them instead!
SELECT *
FROM main_table
LEFT OUTER JOIN (SELECT * FROM table1 LIMIT 100)a ON main_table.id = a.mainTableId
LEFT OUTER JOIN (SELECT * FROM table2 LIMIT 100)b ON main_table.id = b.mainTableId
...

MYSQL - How do I create a single query to accomplish this

I have a fairly complex situation where I'd like to see if there is a single query I can use to get the data.
In the following pseudo code, assume the "*" in each select clause has the appropriate columns selected and that table3 and table4 have similar enough columns that I can handle the diffs with padding/fake columns.
select * from table1 where ActiveFlag=1
for each row returned (as A)
//check if member has a record in membership table
select * from table2 where MembershipId=A.MembershipId
if (row exists)
select * from table3 where MemberId=A.MemberId
else
select * from table4 where MembershipId=A.MembershipId
end of for loop
This assumne each row in TableA can have 0-1 membership, otherwise we need to filter duplicates first.
SELECT CASE WHEN M.MembershipId IS NULL
THEN T4.field1
ELSE T3.field1
END as field1,
....
CASE WHEN M.MembershipId IS NULL
THEN T4.fieldN
ELSE T3.fieldN
END as fieldN
FROM TableA A
LEFT JOIN Membership M
ON A.MembershipId = M.MembershipId
CROSS JOIN table3 T3
CROSS JOIN table4 T4
WHERE A.MemberId = T3.MemberId
OR A.MembershipId = T4.MembershipId
If you have duplicate Membership replace the first two JOIN with something like this.
FROM (
SELECT DISTINCT TableA.*, M.MembershipId
FROM TableA A
LEFT JOIN Membership M
ON A.MembershipId = M.MembershipId
) A
CROSS JOIN ..

MySQL select count of another table's rows where the IDs match

I'm trying to select from one table with the count of another table where the id matches the original tableID sort of like:
select *, (count(0) from table2 where table2.table1ID = table1.table1ID) count
from table1
What's the mySQL syntax for this?
SELECT COUNT(b.*) FROM table1 as a
LEFT JOIN table2 as b ON a.tableID = b.tableID
select
table1.*,
if(table2.table1ID is null,0,count(*))
from
table1
left join table2 on table1.table1ID = table2.table1ID
group by
table1.table1ID;

How to combine 2 columns from 2 tables and subtract duplicates

I have 2 tables t1 and t2. Each have a customer ID column. What I am looking for is to join the 2 columns and SUBTRACT the duplicates.
My EG:
Table1 and Table2 with the IDs for each
I have tried a union query. The result I am left with is ID = 1,2,3,4,5,6,7,8,9,10. Where, what I'm after is subtracting 1-5 from Table2 and the result = 6,7,8,9,10.
I hope that makes sense and that someone is able to help. Sorry if this is a bit too simple compared to what you're all used to.
In SQL Server you can use the EXCEPT operator:
select ID
from Table2
except
select ID
from Table1
Mysql does not support it though. Using a an in clause or a left join would work in both servers:
--Using In clause
SELECT ID
FROM Table2
WHERE ID NOT IN
(
SELECT ID
FROM Table1
);
--Using join
SELECT Table2.ID
FROM Table2
left join Table1
on Table2.ID = Table1.ID
where Table1.ID is null
Use left outer join
select * from t1 left outer join t2 on t1.customerid = t2.customerid

3 tables and 2 left joins

Query 1:
SELECT sum(total_revenue_usd)
FROM table1 c
WHERE c.irt1_search_campaign_id IN (
SELECT assign_id
FROM table2 ga
LEFT JOIN table3 d
ON d.campaign_id = ga.assign_id
)
Query 2:
SELECT sum(total_revenue_usd)
FROM table1 c
LEFT JOIN table2 ga
ON c.irt1_search_campaign_id = ga.assign_id
LEFT JOIN table3 d
ON d.campaign_id = ga.assign_id
Query 1 gives me the correct result where as I need it in the second style without using 'in'. However Query 2 doesn't give the same result.
How can I change the first query without using 'in' ?
The reason being is that the small query is part of a much larger query, there are other conditions that won't work with 'in'
You could try something along the lines of
SELECT sum(total_revenue_usd)
FROM table1 c
JOIN
(
SELECT DISTINCT ga.assign_id
FROM table2 ga
JOIN table3 d
ON d.campaign_id = ga.assign_id
) x
ON c.irt1_search_campaign_id = x.assign_id
The queries do very different things:
The first query sums the total_revenue_usd from table1 where irt1_search_campaign_id exists in table2 as assign_id. (The outer join to table3 is absolutely unnecessary, by the way, because it doesn't change wether a table2.assign_id exists or not.) As you look for existence in table2, you can of course replace IN with EXISTS.
The second query gets you combinations of table1, table2 and table3. So, in case there are two records in table2 for an entry in table1 and three records in table3 for each of the two table2 records, you will get six records for the one table1 record. Thus you sum its total_revenue_usd sixfold. This is not what you want. Don't join table1 with the other tables.
EDIT: Here is the query using an exists clause. As mentioned, outer joining table3 doesn't alter the results.
Select sum(total_revenue_usd)
from table1 c
where exists
(
select *
from table2 ga
-- left join table3 d on d.campaign_id = ga.assign_id
where ga.assign_id = c.irt1_search_campaign_id
);