MySQL Outer join over multible Table - mysql

I need a join over mutible tables in my DB, i cant simply use inner joins, because not every entry has an correspond in all the other table and then a null is fine. The only exception from that are the table5 - table4 connection and the table2 - table 3 connection. I hope the request isnt stupid because i overlook something.
Layout of the DB

First , join table 4 & 5 in a view
CREATE VIEW table4join5 AS
SELECT *
FROM table4
INNER JOIN table5
ON table4.ID_3 = table5.ID_3
Do the same for table 2 & 3
CREATE VIEW table2join3 AS
SELECT *
FROM table2
INNER JOIN table3
ON table2.ID_2 = table3.ID_2
And now you can join it all :
SELECT *
FROM table1
INNER JOIN table2join3 ON table2join3.id = table1.id
INNER JOIN table4join5 ON table4join5.id = table1.id
INNER JOIN table6 ON table6.id = table1.id
INNER JOIN table8 ON table8.id = table1.id

Related

How in inner join few table consecutively in single sql [duplicate]

This question already has answers here:
Joining three tables using MySQL
(11 answers)
Closed 3 years ago.
i am currently have few 5 table , and i wan to inner join table 1 to table 2, and then table to 2 to table 3, and so on....
i have tried this with few separated inner join, but how to get the same result with only 1 sql
SELECT table1.CW_S_EVT,table2.CW_S_TYP, table2.CW_S_SERVER
FROM table1 INNER JOIN table2
ON table1.CW_S_EVT = table2.CW_S_EVT;
SELECT table2.CW_S_EVT,table2.CW_S_TYP, table3.CW_S_TPL
FROM table2 INNER JOIN table3
ON table2 .CW_S_TYP = table3.CW_S_TYP
SELECT table3.CW_S_TPL, table4.CW_L_TPL,table4.CW_CONTENT
FROM table3 INNER JOIN table4
ON table3.CW_S_TPL = table4.CW_S_TPL
SELECT table1.CW_S_EVT,table5.CW_S_VAR, table5.CW_L_VAR
FROM table1 INNER JOIN table5
ON table1.CW_S_EVT = table5.CW_S_EVT;
This is the code i found on internet but it does not match my case.
SELECT *
FROM table1
INNER JOIN table2
ON table1.primaryKey=table2.table1Id
INNER JOIN table3
ON table1.primaryKey=table3.table1Id
What you need is a left join your tables on your criteria. Since this will result to null values, adding coalesce function will be able to help us give those values that matches your criteria.
Additional distinct keyword if its needed.
SELECT distinct coalesce(table1.CW_S_EVT, table2.CW_S_EVT, table3.CW_S_TPL)
, coalesce(table2.CW_S_TYP, table4.CW_L_TPL, table5.CW_S_VAR)
, coalesce(table2.CW_S_SERVER, table3.CW_S_TPL, table4.CW_CONTENT, table5.CW_L_VAR)
FROM table1
LEFT JOIN table2 ON table1.CW_S_EVT = table2.CW_S_EVT;
LEFT JOIN table3 ON table2.CW_S_TYP = table3.CW_S_TYP
LEFT JOIN table4 ON table3.CW_S_TPL = table4.CW_S_TPL
LEFT JOIN table5 ON table1.CW_S_EVT = table5.CW_S_EVT;
Try this
SELECT
table1.CW_S_EVT as t1_CW_S_EVT,
table2.CW_S_TYP as t2_CW_S_TY,
table2.CW_S_SERVE as t2_CW_S_SERVE,
table2.CW_S_EVT as t2_CW_S_EVT,
table3.CW_S_TPL as t3_CW_S_TPL,
table4.CW_L_TPL as t4_CW_L_TPL,
table4.CW_CONTENT as t4_CW_CONTENT,
table5.CW_S_VAR as t5_CW_S_VAR,
table5.CW_L_VAR as t5_CW_L_VAR
FROM
table1, table2, table3, table4, table5
where
table1.CW_S_EVT = table2.CW_S_EVT AND
table2 .CW_S_TYP = table3.CW_S_TYP AND
table3.CW_S_TPL = table4.CW_S_TPL AND
table1.CW_S_EVT = table5.CW_S_EVT;
SELECT table1.CW_S_EVT,table3.CW_S_TYP,table4.CW_S_TPL, table5.CW_S_VAR
FROM table1
INNER JOIN table2 ON table1.CW_S_EVT = table2.CW_S_EVT
INNER JOIN table3 ON table2.CW_S_TYP = table3.CW_S_TYP
INNER JOIN table4 ON table3.CW_S_TPL = table4.CW_S_TPL
INNER JOIN table5 ON table1.CW_S_EVT = table5.CW_S_EVT
Thanks for all the kind answer, but i found this working at last, juz record here maybe for future use.^^

inner join two tables having null values on the foreign keys of each other

I have the following data in the database in table1
I run the following query and returns nothing
SELECT
someColumns
FROM table1
INNER JOIN table2 ON table1.id = table2.id
INNER JOIN table3 ON table1.anotherId = table3.anotherId
I want to return all the records in (table2 with id = table1.id and table3 with anotherId = table1.anotherId)
You should using LEFT OUTER JOIN to get the specified result.

MySQL How to do a 3 table join

I'm trying to perform a 3 table join on MySQL in order to achieve something like the diagram below.
The main problem I'm having is that I only want to work with the records of table A which has 100 records so if there are no relationships for the right tables I would like to see a null.
This all works fine when only table A and B are involved but when I try to do the third join with C I'm getting more than the original 100 records, I'm getting 130 which I believe is because is adding the records that match B-C with duplicate data from table A.
What am I missing?
This is the SQL I currently have that returns correctly 100 records
SELECT count(A.id)
FROM tableA A
LEFT JOIN TableB B ON B.id = A.b_id
This is what I'm trying to do that is returning more than the original 100 records for Table A.
SELECT count(A.id)
FROM tableA A
LEFT JOIN TableB B ON B.id = A.b_id
LEFT JOIN TableC C ON C.id = B.c_id
This could be resolved by a JOIN to a subquery rather than a table.
If you had unique Ids to join to, it would simply be like you've tried already (arbitrary example):
SELECT * from table1 t1
LEFT JOIN table2 t2 on t2.id = t1.id
LEFT JOIN table3 t3 on t3.id = t2.id
If, however the id field in table3 wasn't unique, you'd get multiple rows for each duplicate. You could resolve this by:
SELECT * from table1 t1
LEFT JOIN table2 t2 on t2.id = t1.id
LEFT JOIN (SELECT * FROM table3 GROUP BY id) t3 on t3.id = t2.id
So, using your example (assuming only the third join has duplicates), something like:
SELECT count(A.id)
FROM tableA A
LEFT JOIN TableB B ON B.id = A.b_id
LEFT JOIN (SELECT * FROM TableC GROUP BY id) C ON C.id = B.c_id
...should do the trick. This is down to assumption of your table and data structure, so you might want to make the asterisk more explicit.
SELECT count(distinct A.id)
FROM tableA A
LEFT JOIN TableB B ON B.id = A.b_id
LEFT JOIN TableC C ON C.id = B.c_id

cant get sql table joining to work

I have 3 tables that i want to run a query on, what im trying to do is as follows:
TABLE1.name , TABLE1.description, TABLE2.category.
The link between TABLE2 and TABLE1 is the product_ID and category_ID which both are on TABLE3
So here's the query im trying to run
SELECT table1.name AS product_name,
table1.description AS product_description,
table2.name AS product_category
table3.product_id
FROM table1
INNER JOIN table1 a ON table3.product_id = table1.product_id
INNER JOIN table2 b ON table3.category_id = table2.category_id
INNER JOIN table2 ON table1.product_id = table2.product_id;
The structure of the tables is as follows :
Table1(product_id,name,description)
Table2(category_id,name2)
Table3(product_id,category_id)
If you use assign alias (table1 a, table 2 b) to table then use it otherwise don't assign.
You are Joining two time table 2 but not table 3 and assuming that table2 and table1 are related by category_id
and table3 table1 are related by both product_id and category_id
SELECT
table1.name AS product_name
,table1.description AS product_description
,table2.name AS product_category
,table3.product_id
FROM table1
INNER JOIN table2 table1.category_id = table2.category_id ;
INNER JOIN table3 ON table3.product_id = table1.product_id
and table3.category_id = table1.category_id
where is your condition of Table1.category_id = Table2.category_id ?

Mysql select multiple tables but inner join on one table

Is it possible to select multiple tables and make inner join on one of those tables? e.g.
SELECT *
FROM table1 AS t1, table2 AS t2, table3 AS t3
INNER JOIN table4 AS t4 ON t1.row3 = t4.row3
INNER JOIN table5 AS t5 ON t1.row4 = t5.row4
WHERE ...
This paricular case is causing me a problem. it gives me an error - Unknown column "t1.row3" in 'on clause'. I don't know if it's possible to select multiple tables but make inner join on one of those tables.
The JOIN operand has higher precedence than comma , operand, so the join is effectively treated as
t1, t2, (t3, t4, t5 ON ... )
Put parentheses around t1, t2, t3.
SELECT *
FROM ( table1 AS t1, table2 AS t2, table3 AS t3 )
INNER JOIN table4 AS t4 ON t1.row3 = t4.row3
INNER JOIN table5 AS t5 ON t1.row4 = t5.row4
WHERE ...
You can also write your query as:
SELECT *
FROM table1 AS t1
INNER JOIN table2 AS t2
INNER JOIN table3 AS t3
INNER JOIN table4 AS t4 ON t1.row3 = t4.row3
INNER JOIN table5 AS t5 ON t1.row4 = t5.row4
WHERE ...
because comma is equivalent to INNER JOIN without a join condition.