SQL JOIN inside LEFT JOIN - mysql

Is it possible to join a table inside a table join? If so how do I do it?
I have experience doing standard joins (LEFT JOIN) and have worked out how to join multiple tables but cant see how to join a table inside a table join. Here is my data structure to explain better:
T1 (
T1_T2
)
T1_T2 (
T1_T2_id
T1_T2_T3
)
T1_T2_T3 (
T1_T2_T3_id
T1_T2_T3_a
T1_T2_T3_b
)
Currently my SQL looks like this:
SELECT * FROM T1
LEFT JOIN T1_T2
ON T1.T1_T2 = T1_T2.T1_T2_id
This returns the data I want from T1 and T1_T2. I want to join T1_T2 on to T1_T2_T3 like so:
SELECT * FROM T1_T2
LEFT JOIN T1_T2_T3
ON T1_T2.T1_T2_T3 = T1_T2_T3.T1_T2_T3_id
Can I do this in one query?

I guess you are looking for something like this:
SELECT * FROM T1
LEFT JOIN T1_T2
ON T1.T1_T2 = T1_T2.T1_T2_id
LEFT JOIN T1_T2_T3
ON T1_T2.T1_T2_T3 = T1_T2_T3.T1_T2_T3_id

Related

LEFT OUTER JOIN and WHERE EXISTS. Are they equivalent?

I would like to create equivalent MySQL query using LEFT OUTER JOIN to WHERE EXISTS. I am following this question:
Are the SQL concepts LEFT OUTER JOIN and WHERE NOT EXISTS basically the same?
This is the original query:
SELECT *
FROM tableA
JOIN tableB ON tableA.tableA_id = tableB.tableB_id
JOIN tableC ON tableC.tableC_id = tableB.tableB_id
WHERE NOT EXISTS (
SELECT 1
FROM tableD
WHERE tableA.employee_id = tableD.employee_id AND tableC.tableC_datum = DATE(tableD.tableD_od_datetime)
)
But this query return different values:
SELECT *
FROM tableA
JOIN tableB ON tableA.tableA_id = tableB.tableB_id
JOIN tableC ON tableC.tableC_id = tableB.tableB_id
LEFT OUTER JOIN tableD ON tableA.employee_id = tableD.employee_id AND tableC.tableC_datum = DATE(tableD.tableD_od_datetime)
WHERE tableD.employee_id IS NULL AND DATE(tableD.tableD_od_datetime) IS NULL
Why are these two outputs not equivalent, please?
The not exists and left join ... rgt.col is null approaches are identical. The left join however will contain columns from the unwanted table so just be specific with the select clause:
SELECT table_a.*, table_b.*, table_c.*
FROM table_a
JOIN table_b ...
JOIN table_c ...
LEFT JOIN table_d ...
I would rather avoid * at all and explicitly list exactly those columns that I need.

Searching from three different tables with three different columns

How to search three different tables with three different columns? The current command:
$sql="select t1.brand_name,t2.category_name from brand_data_add AS t1
LEFT JOIN category_add_data AS t2 ON t1.brand_name=t2.category_name
UNION select t1.brand_name,t2.category_name from brand_data_add AS t1
RIGHT JOIN category_add_data AS t2 ON t1.brand_name=t2.category_name";
SQL:
SELECT
*
FROM
(
SELECT
workouts.name,
workouts.description,
`user`.user_email
FROM
workouts
LEFT JOIN `user` ON
workouts.created_by = `user`.iduser
UNION
SELECT
workouts.name,
workouts.description,
`user`.user_email
FROM
workouts
RIGHT JOIN `user` ON
workouts.created_by = `user`.iduser) AS main_table
WHERE
user_email LIKE '%gmail%';
Explanation:
You should enclose your union query with bracket
Fetch the fields with SELECT clause
Use the WHERE clause to do the conditional filter in virtual table main_table (union of two table)
select t1.brand_name,t2.category_name, t3.? from brand_data_add AS t1
LEFT JOIN category_add_data AS t2 ON t1.brand_name=t2.category_name
LEFT JOIN t3 on t3 on t3.? = t?
UNION select t1.brand_name,t2.category_name, t3.? from brand_data_add AS t1
RIGHT JOIN category_add_data AS t2 ON t1.brand_name=t2.category_name"
RIGHT JOIN t3 on t3 on t3.? = t?

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

Crosstab query in mysql using phpmyadmin

I have 2 tables in mysql, tbl_post & tbl_comment,
I require a crosstab query , based upon tbl_post.post_id, the result should be like
all elments of tbl_post + count of records form tbl_comment, where
tbl_post.post_id == tbl_comment.post_id
e.g. Result should be like ::
post_id,title,content,tags,status,create_time,update_time,author_id,likes + count from tbl_comment
Please see the image.
I am new to sql just having academic knowledge , and couldn't figure it out. Any help is appreciated.
I think you just need to join tbl_post to a subquery which counts the number of comments for each post.
SELECT t1.*,
COALESCE(t2.post_count, 0) AS post_count
FROM tbl_post t1
LEFT JOIN
(
SELECT post_id, COUNT(*) AS post_count
FROM tbl_comment
GROUP BY post_id
) t2
ON t1.post_id = t2.post_id
If you want to create a view using the above query then you need to get a bit creative. The following attempt will fail because the above query has a subquery in it:
CREATE VIEW PostCountView AS
SELECT t1.*,
COALESCE(t2.post_count, 0) AS post_count
FROM tbl_post t1
...
Instead, you can create a view for the subquery, and then use that in a second view for the main query:
CREATE VIEW PostCountView AS
SELECT post_id, COUNT(*) AS post_count
FROM tbl_comment
GROUP BY post_id
CREATE VIEW PostCountMainView AS
SELECT t1.*,
COALESCE(t2.post_count, 0) AS post_count
FROM tbl_post t1
LEFT JOIN PostCountView t2
ON t1.post_id = t2.post_id
select t1.post_id,
t1.title,
t1.content,
t1.tags,
t1.status,
t1.create_time,
t1.updated_time,
t1.author_id,
t1.likes,
count(t2.post_id)
from tbl_post t1
LEFT JOIN tbl_comment t2
on t1.post_id = t2.post_id
group by t1.post_id;

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
)