3 tables and 2 left joins - mysql

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
);

Related

More rows after Left Join in MySQL

I've two tables, table1 contains 22780 rows. Now I left join table1 with table2 (which doesn't contain any duplicates) and I get 23588 rows.
SELECT * FROM Table1
left join Tabelle6 ON CAST(Table1.Customer AS Int) = table2.Customer
Why do I get more rows now? I only need every row from table1 once.
Edit: found my issue, table 2 does contain duplicates. But is there any way to join every row only once and ignore any further matches?
As the comment suggests, the easiest way to handle this would probably be to do SELECT DISTINCT to remove duplicates from your result set:
SELECT DISTINCT
t1.col1,
t1.col2,
t1.Customer,
...
FROM Table1 t1
LEFT JOIN Table2 t2
ON CAST(t1.Customer AS Int) = t2.Customer
But there is another option here. We could also join to a subquery which removes duplicate customers. This would ensure that no record from the first table gets duplicated from matching to more than one record in the second table.
SELECT *
FROM Table1 t1
LEFT JOIN
(
SELECT DISTINCT Customer
FROM Table2
) t2
ON CAST(t1.Customer AS Int) = t2.Customer

Mysql need to get one column two times from table1 and sort them by two different columns of table2

So here is the thing, I have two tables:
table1 has columns intUsersID, varUsersName
table2 has columns intCouriers, intResponsible
intCouriers (have some numbers of intUsersID that are Couriers), and intResponsible (have some numbers of intUsersID that are Responsible)
In my query I must see User Names of Couriers and of the Responsible persons
something like that:
SELECT
table1.varUsersName 'Couriers',
table1.varUsersName 'Responsible'
FROM
table1
LEFT JOIN
table2 ON table2.intCouriers = table1.intUsersID
And then I need some how to subquery or join this "table1.varUsersName 'Responsible'", to get also 'Reponsible' persons. Please help me.
Should be this
SELECT table1.varUsersName 'Couriers', table2.varUsersName 'Responsible'
FROM table1
INNER JOIN table3 on table1.intUsersID = table3.intCouriers
INNER JOIN table1 as Table2 on table2.intUsersID = table3. intResponsible
SELECT Couriers.varUsersName as "Couriers",
Responsible.varUsersName as "Responsible"
FROM `table2` t2
LEFT JOIN table1 Couriers on Couriers.intUsersID = t2.intCouriers
LEFT JOIN table1 Responsible on Responsible.intUsersID = t2.intResponsible

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 Table Inner Join Query in MySQL

How would I go about writing a query in MySQL on 3 different tables? Here's what I have so far:
SELECT distinct cool_id, example, table1.name
FROM tardis
INNER JOIN table1
ON table1.unique_id = table2.unique_id
AND table1.unique_id='12345'
AND table2.status='active'
Now let's say that there is a column called 'planets' that exists in a 3rd table. How would I add that to this query to select 'planets' in addition to matching the other conditions in my current query? Also, please advise if an INNER JOIN is not the best choice for this.
Just add another INNER JOIN clause:
SELECT columns
FROM table1
INNER JOIN table2 ON table1.col1 = table2.col2
INNER JOIN table3 ON table1.somecol = table3.othercol
The ON condition in the second join can refer to columns from table1, table2, or both.

Mysql Puzzle : Conditions in ON vs. WHERE

The following two queries do not return the same result. Why ?
Note : I found this question as a Mysql puzzle, I don't have more data on this question?
SELECT table1.*
FROM table1 LEFT JOIN table2
ON table2.table1_id = table1.id
AND table2.val < 5
SELECT table1.*
FROM table1 LEFT JOIN table2
ON table2.table1_id = table1.id
WHERE table2.val < 5
The left outer join will return rows (with null columns from table2 if they were selected) when the condition isn't met, whereas the WHERE filters them out.
The first query will return all rows from table 1. A LEFT JOIN always returns all rows from the left table regardless of what you write in the join condition (it can however duplicate rows if there are multiple matches, but since you are joining on a field called id, it is most likely a unique key, so there will be no duplicates).
The second query will only return those rows from table 1 where the corresponding row in table2 has val < 5. You could in fact have written INNER JOIN here instead of LEFT JOIN because the rows where the join fails will not be in the result set anyway due to the WHERE clause.
First query only joins if table2.val < 5. Second filters even table1 rows. Identical result should be given if you use INNER JOIN instead.
Trying to think as a "compiler" look the parenthesis...
SELECT table1.*
FROM table1
LEFT JOIN table2 ON (
table2.table1_id = table1.id
AND table2.val < 5
)
This example does the LEFT JOIN of 2 tables when the condition table2.table1_id = table1.id AND table2.val < 5 is true.
SELECT table1.*
FROM table1
LEFT JOIN table2 ON ( table2.table1_id = table1.id )
WHERE (table2.val < 5)
This example do the LEFT JOIN when the condition table2.table1_id = table1.idapplies and then get the rows of the result of table1 LEFT JOIN table2 ON ( table2.table1_id = table1.id ) WHERE the condition table2.val < 5 is true