MySQL JOIN the result of UNION - mysql

Is it possible to join the result of UNION of two tables with the 3rd table? Something like this:
(SELECT DISTINCT `Fund_ID`, `Fund_Name` FROM `admin`
UNION
SELECT `Fund_ID`,`Fund_Name` FROM `admin_custom` WHERE `admin_custom`.`user_id` = 361) a
LEFT JOIN `qt1`
ON `qt1`.`Fund ID` = a.`Fund_ID`
but this code doesn't work. I could move JOIN inside of each SELECT query before UNION, but would rather try to JOIN with the UNION result.
How can I fix this?

Yes, it is possible. However, your code is incorrect since you're missing SELECT statement itself, because your first select becomes a rowset (runtime-created table). I.e. you have to specify SELECT operator and fields that you want to get. Simplest case:
SELECT
a.*
FROM
(SELECT DISTINCT `Fund_ID`, `Fund_Name` FROM `admin`
UNION
SELECT `Fund_ID`,`Fund_Name` FROM `admin_custom` WHERE `admin_custom`.`user_id` = 361) AS a
LEFT JOIN `qt1`
ON `qt1`.`Fund ID` = a.`Fund_ID`

SELECT * FROM
(SELECT DISTINCT `Fund_ID`, `Fund_Name` FROM `admin`
UNION
SELECT `Fund_ID`,`Fund_Name` FROM `admin_custom` WHERE `admin_custom`.`user_id` = 361) a
LEFT JOIN `qt1`
ON `qt1`.`Fund ID` = a.`Fund_ID`;

Related

Wrong count result in mysql when joining two tables

I am trying to join two tables and get the count and grouped by specific field. However, it outputs same count values even if the other table consist only two rows. How should I fix this?
Here's my code:
SELECT tbl1.preferredDay, COUNT(tbl1.preferredDay) as count_1, COUNT(tbl2.preferredDay) as count_2
FROM tblschedule as tbl1
LEFT JOIN tblappointments as tbl2 ON (tbl1.preferredDay = tbl2.preferredDay)
WHERE tbl1.preferredDay = tbl2.preferredDay
GROUP BY preferredDay;
Here is the output but it should be [15, 0][3, 3]
Your query is based on left join it will return the same count().
This is a working query for Mysql 8:
with tbl1 as (
SELECT preferredDay, count(1) as count_1
FROM tblschedule
GROUP BY preferredDay
),
tbl2 as (
SELECT preferredDay, count(1) as count_2
FROM tblappointments
GROUP BY preferredDay
)
select t1.preferredDay, t1.count_1, t2.count_2
from tbl1 t1
inner join tbl2 t2 on t1.preferredDay = t2.preferredDay
There are two WITHs to get separately the count and then an INNER JOIN to join those results
For Mysql 5.7 and lower :
select t1.preferredDay, t1.count_1, t2.count_2
from (
SELECT preferredDay, count(1) as count_1
FROM tblschedule
GROUP BY preferredDay
) as t1
inner join (
SELECT preferredDay, count(1) as count_2
FROM tblappointments
GROUP BY preferredDay
) as t2 on t1.preferredDay = t2.preferredDay

MySQL problem joining 2 tables with UNION

I'm doing UNION in MySQL that I'm unable to troubleshoot for a while.
Error says that
syntax is incorrect around t1.*
Those 2 SELECTs work ok separately, checked. But UNION fails. I'm not custom to MySQL syntax, maybe something is wrong with that.
SELECT (
t1.*,
a.region_count
FROM
(
SELECT
data_region,
COUNT(*) AS region_count
FROM
t2
GROUP BY
data_region
) AS a
LEFT OUTER JOIN
t1
ON
t1.values_att0 = a.data_region
WHERE
t1.name_0 = 'region'
) AS b
UNION
SELECT (
t1.*,
c.age_gen_count
FROM
(
SELECT
data_dage,
data_gen,
COUNT(*) AS age_gen_count
FROM
t2
GROUP BY
data_dage,
data_gen
) AS c
LEFT JOIN
t1
ON
t1.values_att0 = c.data_dage AND
t1.id_question_1 = c.data_gen
WHERE
t1.name_0 = 'age' AND
t1.q_name_1 = 'gen'
)
You are using parenthesis around your SELECT field, this is your syntax error origin (the UNION is not the cause). Just remove them:
SELECT
t1.*,
a.region_count
FROM
(
SELECT
data_region,
COUNT(*) AS region_count
FROM t2
GROUP BY data_region
) AS a
LEFT OUTER JOIN t1
ON t1.values_att0 = a.data_region
WHERE t1.name_0 = 'region'
UNION ALL
SELECT
t1.*,
c.age_gen_count
FROM
(
SELECT
data_dage,
data_gen,
COUNT(*) AS age_gen_count
FROM t2
GROUP BY data_dage, data_gen
) AS c
LEFT JOIN t1
ON t1.values_att0 = c.data_dage
AND t1.id_question_1 = c.data_gen
WHERE t1.name_0 = 'age'
AND t1.q_name_1 = 'gen'

MYSQL LEFT JOIN results with tables?

After SELECT a table GROUP BY a field, I get a results, and then I LEFT JOIN the results with a table. Can I do that?
Thankyou!
You should be, try:
Select *
From(
SELECT *
FROM myTableOne
GROUP BY myCol1
) firstTable
LEFT JOIN myTableTwo secondTable ON
firstTable.idColumn = secondTable.idColumn
Where myTableOne is the group by selection and myTableTwo is the table you want to left join on

Mysql UNION as a subquery with an alias field

I have a UNION query as bellow (I have simplified my working query so it is easier to read) :
SELECT count(*) FROM
(SELECT DISTINCT `tableA`.`Store Name` FROM `tableA` UNION SELECT DISTINCT `tableB`.`Store Name` FROM `tableB`) t
This works fine and results in a single number with column name COUNT(*)
I want to get this value as another column in another query so I do :
SELECT DISTINCT `tableC`.`id as PID,
(SELECT count(*) from (SELECT DISTINCT `tableA`.`Store Name` FROM `tableA` UNION SELECT DISTINCT `tableB`.`Store Name` FROM `tableB`) t) AS noofstores
WHERE
.....;
But it wont work! What am I doing wrong? This is part of a bigger query, and all the other subqueries work fine when I do
,
(SELECT .... ) AS column_name
,
Sorry for poor error description. Update :
This is my full query :
SELECT DISTINCT
`tableC`.`id` as PID,
(SELECT count(*)
from
(SELECT DISTINCT `tableA`.`Store Name` FROM `tableA` WHERE `tableA`.`id` = PID
union
SELECT DISTINCT `tableB`.`Store Name` FROM `tableB` WHERE `tableB`.`id` = PID) t) AS mycolumn_name
FROM
`tableC`
Looks like I had the union right and all, but the problem is the PID I am reffering to in the union :
1054 - Unknown column 'PID' in 'where clause'
So how do I solve this?
The PID column does not exist in the inner subquery, only in the outer query. Either you do an inner join in both of the queries in the union on tableC and do the filtering there, or you need to return the id column in the union queries and join the PID on them to do the filtering.
select tableC.id as PID, count(distinct storename)
from
(select distinct id, storename from tableA
union
select distinct id, storename from tableB) t1
inner join tableC on t1.id=tableC.id
group by tableC.id
You have to join thos two tables to get the result
SELECT DISTINCT c.id as PID from table C inner join
(SELECT count(*) from
(SELECT DISTINCT `tableA`.`Store Name` as st
FROM `tableA` UNION SELECT DISTINCT `tableB`.`Store Name` as st
FROM `tableB`) t on t.some_id=c.id WHERE

Mysql - Left Join all tables

I have a query that looks like:
SELECT 'asdf', '123' ...
FROM table1
LEFT JOIN table2
on
(
condition1
)
LEFT JOIN table3
on
(
condition2
)
where
(
main_condition
)
Now the problem is, I need to conditionally include table1 as well. I tried this:
..
..
FROM table1
on
(
new_condition
)
..
..
but it wouldn't work. Please help.
EDIT (New finding):
In this post (http://blog.sqlauthority.com/2010/07/20/sql-server-select-from-dual-dual-equivalent/), I found this piece of code:
SELECT 1 as i, f.bar, f.jar FROM dual LEFT JOIN foo AS f on f.bar = 1 WHERE dual.dummy = ‘X’
UNION
SELECT 2 as i, f.bar, f.jar FROM dual LEFT JOIN foo AS f on f.bar = 2 WHERE dual.dummy = ‘X’
I'm sure it's not directly related to what I'm trying to do, but is it possible to JOIN a table to DUAL like that?
Dummy table:
Select a record from a dummy table first. dual is such a table, that is built in in MySQL for this exact purpose. I wrapped dual in a subselect, because MySQL apparently doesn't allow left joining against it.
SELECT 'asdf', '123' ...
FROM
(select 1 from dual) d
LEFT JOIN table1
on(
new_condition
)
LEFT JOIN table2
on
(
condition1
)
LEFT JOIN table3
on
(
condition2
)
Full (outer) join
Another solution, though different is using a full join or full outer join, which is like a left join and right join combined. It is quite different, though you can achieve a very similar result:
select
*
from
table1
full outer join table2 on joincondition.
In the query above, all records from both tables are returned, even if no matching record in either table exists.
Thanks for contributing to the discussion. I found the answer. It's really simple:
SELECT temp_table.* FROM
(SELECT 'asdf', '123' ... FROM DUAL) temp_table
LEFT JOIN table1
on
(
new_condition
)
LEFT JOIN table2
on
(
condition1
)
LEFT JOIN table3
on
(
condition2
)
where
(
main_condition
)
Interesting problem. Maybe I should favorite my own question this time :)
You need to include the condition in the on clause for the first join:
SELECT 'asdf', '123' ...
FROM table1 LEFT JOIN
table2
on condition1 AND new condition LEFT JOIN
table3
on condition2
where main_condition
When using a where clause with left join be careful. Normally, you want to move these conditions into the on clauses, because they can inadvertently undo the effect of the left outer join (turning it into an inner join).
you cant make this new condition in ON clause
on clause is just when you join, but you can add this new condition in where clause
example
where
(
main_condition
)
AND
(
new condition
)
EDIT:
try this
SELECT 'asdf', '123' ...
FROM (select 'asdf', '123' ... FROM table1 WHERE new_condition ) t
^^--your new condition here
LEFT JOIN table2
on
........
EDIT2: if your new condition can be wrong you can make an if statment
where
(
main_condition
)
AND
(
if(new condition is something , do something , else do something else)
)
edit3:
SELECT 'asdf', '123' ...
FROM (select 'asdf', '123' ... FROM table1 where main condition
UNION
select 'asdf', '123' ... FROM table1 WHERE new_condition ) t
^^--your new condition here
LEFT JOIN table2
on
........
My best guess with comments to date.
SELECT 'asdf', '123' ...
FROM table1
FULL OUTER JOIN table2 --NOTE THE FULL OUTER here all records in table 2 and only those that match in table 1
on
condition1 AND
new_condition=True
LEFT JOIN table3
on
(
condition2
)
where
(
main_condition
)