How can I do a FULL JOIN of MULTIPLE TABLES in MySQL - mysql

we have been searching for it but all we see is 2 tables by the left and right inner/outer joins.
I love you guys.

MySQL doesn't support FULL OUTER JOIN.
As you mention, you can simulate a FULL OUTER JOIN of two tables using a combination of LEFT and RIGHT OUTER joins.
SELECT * FROM tableA LEFT JOIN tableB ON tableA.b_id = tableB.id
UNION ALL
SELECT * FROM tableA RIGHT JOIN tableB ON tableA.b_id = tableB.id
WHERE tableA.b_id IS NULL
The same technique can in theory be extended to more than two tables. I'd suggest first using the above approach to join two of the tables as a view. Then use the same approach again to join the view to the third table.

I don't know what to say about the love part, but
Having tables named a and b:
SELECT a.*, b.* FROM a, b
Does this the trick?

Related

Can't understand. Is this a subquery?

I have something in a query that I have to edit, that I don't understand.
There are 4 tables that are joined: tickets, tasks, tickets_users, users. The whole query is not important, but you have an example at the end of the post. What bugs me is this kind of code used many times in relation to other tables:
(SELECT name
FROM users
WHERE users.id=tickets_users.users_id
) AS RequesterName,
Is this a subquery with the tables users and tickets_users joined? What is this?
WHERE users.id=tickets_users.users_id
If this was a join I would have expected to see:
ON users.id = tickets_users.users_id
And how is this different from a typical join? Just use the same column definition: users.name and just join with the users table.
Can anyone enlighten me on the advanced SQL querying prowess of the original author?
The query looks like this:
SELECT
description,
(SELECT name
FROM users
WHERE users.id = tickets_users.users_id) AS RequesterName,
(SELECT description
FROM tickets
WHERE tickets.id = ticket_tasks.tickets_id) AS TicketDescription,
ticket_tasks.content AS TaskDescription
FROM
ticket_tasks
RIGHT JOIN
tickets ON ticket_tasks.tickets_id = tickets.id
INNER JOIN
tickets_users ON tickets_users.tickets_id = tickettasks.tickets_id
Thanks,
This is what is called a correlated subquery. To describe it in simple terms its doing a select inside a select.
However doing this more than once in ANY query is not recommended AT ALL.. the performance issue with this will be huge.
A correlated subquery will return a row by row comparison for each row of the select... if that doesnt make sense then think of it this way...
SELECT
id,
(SELECT id FROM tableA AS ta WHERE ta.id > t.id)
FROM
tableB AS t;
This will do for each row in tableB, every row in tableA will be selected and compared to tableB id.
NOTE:
If you have 100 rows in all 4 tables and you do a correlated subquery for each one then you are doing 100*100*100*100 row comparisons. thats 100,000,000 (one hundred million) comparisons!
A correlated subquery is NOT a join, but rather a subquery..
SELECT *
FROM
(SELECT id FROM t -- this is a subquery
) AS temp
However, JOINs are different... generally you can do it one of these two ways
This is the faster way
SELECT *
FROM t
JOIN t1 ON t1.id = t.id
This is the slower way
SELECT *
FROM t, t1
WHERE t1.id = t.id
what the second join is doing is making the Cartesian Product of the two tables and then filtering out the extra stuff in the WHERE clause as opposed to the first JOIN that filters as it joins.
For the different types of joins theres a few and all are useful in their prospective actions..
INNER JOIN (same as JOIN)
LEFT JOIN
RIGHT JOIN
LEFT OUTER JOIN
RIGHT OUTER JOIN
In mysql FULL JOIN or FULL OUTER JOIN does not exist.. so in order to do a FULL join you need to combine a LEFT and RIGHT join. See this link for a better understanding of what joins do with Venn diagrams LINK
REMEMBER this is for SQL so it includes the FULL joins as well. those don't work in MySQL.

How this queries will be evaluated? and, join syntax

How this queries will be evaluated?
(what i ask is: what will be the logic that the db-engine will use to gather the data?)?
A:
SELECT tableA.* FROM tableA
LEFT JOIN tableB ON tableB.key1 = tableA.key1
INNER JOIN tableC ON tableC.key2 = tableB.key2
B:
SELECT tableA.* FROM tableA
INNER JOIN tableC
LEFT JOIN tableB
ON (tableB.key1 = tableA.key1) AND (tableC.key2 = tableB.key2)
C:
What is the syntax, for joining multiple tables? (A and B for example)
D:
What is the logic behind the order of joins?
(How different joins (left, and inner) should be combines in a query?)
ANY-one?
SELECT tableA.* FROM tableA
LEFT JOIN tableB ON tableB.key1 = tableA.key1
INNER JOIN tableC ON tableC.key2 = tableB.key2
Since no brackets are used this should be evaluated left-to-right, so:
SELECT tableA.*
FROM
(tableA LEFT JOIN tableB ON tableB.key1 = tableA.key1)
INNER JOIN tableC ON tableC.key2 = tableB.key2
Meaning you first select all records from table A, with the matching records from B if they exist (outer join). That result set is then joined with C, but since you join on B.Key, all previous records where B = null will now disappear.
I am quite sure that the first join could be an inner join, giving the same result.
SELECT tableA.* FROM tableA
INNER JOIN tableC
LEFT JOIN tableB
ON (tableB.key1 = tableA.key1) AND (tableC.key2 = tableB.key2)
Now we first cross-join every record from A with every record from C (cartesian product).
That (possibly huge and possibly meaningless) resultset we extend with data from B where we can find it (meaning we add a record from B wherever we have a match with either A or B).
In general, when joining several tables, just take it step by step and always try to realize what you are joining with what. When in doubt, use brackets :)

(+) function in PLSQL

I'm now dealing with PLSQL developer, which is my very first time. And I find this kind of query
select * from tableA, tableB
where tableA.field1 = tableB.field1(+)
I'm wondering the function of the (+) in the query. Could you guys be so kind to explain it ?
where tableA.field1 = tableB.field1(+)
This is the old syntax for an outer join, adopted by Oracle, and made redundant when ANSI actually standardised the SQL language. Oracle themselves now suggest you use outer join in preference to this old syntax (from the link below):
Oracle recommends that you use the FROM clause OUTER JOIN syntax rather than the Oracle join operator.
See this entry in the Oracle docs for more detail.
This is Oracle SQL OUTER JOIN syntax
It can be interpreted as
select * from tableA
OUTER JOIN tableB ON tableA.field1 = tableB.field1
From the oracle documentation:
(+) Indicates that the preceding column is the outer join column in a join.
It can be used as
select * from tableA right outer join tableB where tableA.field1 = tableB.field1
(+)operator indicates that it will return all the rows from the right table(matching and non matching) both rows from the right table.
And matching rows are returned from the left table.
If rows are not matching from the right table then it returns null.
+ is used to retrive the mathced and unmached records from the table.
example:
table A and table B
if you are using like A.column1=B.column1(+)
it retrives the unmached records from table A and its called as left outer join.
That's Oracle specific notation for a LEFT OUTER JOIN
Exemple :
select ...
from a,b
where a.id=b.id(+)
The query would be re-written
SELECT ...
FROM a
LEFT JOIN b ON b.id = a.id

Efficient way to simulate full outer join in MySQL?

According to Google search: since MySQL does not support full outer join, it could be simulated via union and/or union all. But both of these either remove genuine duplicates or show spurious duplicates.
What would be correct and efficient way?
This question seems relevant but couldn't get the answer of it.
You can use a LEFT JOIN and a RIGHT JOIN:
SELECT * FROM tableA LEFT JOIN tableB ON tableA.b_id = tableB.id
UNION ALL
SELECT * FROM tableA RIGHT JOIN tableB ON tableA.b_id = tableB.id
WHERE tableA.b_id IS NULL
There is also some information on Wikipedia about this topic: Full outer join.
The Wikipedia article suggests using a UNION in MySQL. This is slightly slower than UNION ALL, but more importantly it won't always give the correct result - it will remove duplicated rows from the output. So prefer to use UNION ALL instead of UNION here.

What's the different between these 2 mysql queries? one using left join

I see people using LEFT JOIN in their mysql queries to fetch data from two tables. But I normally do it without left join. Is there any differences besides the syntax, e.g. performance?
Here's my normal query style:
SELECT * FROM table1 as tbl1, table2 as tbl2 WHERE tbl1.id=tbl2.table_id
as compared to
SELECT * FROM table1 as tbl1 LEFT JOIN table2 as tbl2 on tbl1.id=tbl2.id
Personally I prefer the first style...hmm..
On a left join, all values from table1 are selected even if table2 does not contain the same id.
Your normal query style can be compared to an "inner join".