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.^^
Related
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.
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
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
Is it possible to OR two separate INNER JOIN's so that the result set contains data from either of the two INNER JOIN's? For instance, is the following possible in MySQL.
SELECT * FROM table1
(INNER JOIN table2 ON table1.column_name=table2.column_name)
OR
(INNER JOIN table3 ON table1.column_name=table3.column_name)
No you cant do this way, one way is to use union
SELECT * FROM table1 INNER JOIN table2 ON table1.column_name=table2.column_name
union
SELECT * FROM table1 INNER JOIN table3 ON table1.column_name=table3.column_name
You can use UNION:
SELECT * FROM table1 INNER JOIN table2 ON table1.column_name=table2.column_name
UNION DISTINCT
SELECT * FROM table1 INNER JOIN table3 ON table1.column_name=table3.column_name
As described at https://dev.mysql.com/doc/refman/4.1/en/union.html, UNION combines the results of several selects.
you need use where
select * from table1
where
(table1.column_name = (select table2.column_name from table2 inner join table1.column_name as on table1.column_name = table2.column_name))
OR
(table1.column_name = (select table3.column_name from table3 inner join table1.column_name as on table1.column_name = table3.column_name))
work perfect here!
I have a table(T1) in which 2 columns(X and Y) are id's. The name's of these corresponding id's are in another table (T2) with the column name.
Suppose I was only using X then, a simple Inner join would have solved my problem in getting the name.
Such as
Select T1.somedata,T1.somedata1,T2.name from T1
Inner Join T2 ON T1.X=T2.id
But,what if I want the name's to be resolved for the T1.Y also?, which name would the Inner Join resolve it to ??
Select T1.somedata,T1.somedata1,T2.name from T1
Inner Join T2 ON T1.X=T2.id
Inner Join T2 ON T1.Y=T2.id
The above query is wrong, I know. Can I get the names of those corresponding to both T1.Xand T1.Y with an INNER Join?
-Beginner
I suggest always add aliases to tables and columns. So you will be sure which data are selected.
select
T1.somedata,
T1.somedata1,
T2X.name as XName,
T2Y.name as YName
from T1 as T1
inner join T2 as T2X on T2X.id = T1.X
inner join T2 as T2Y on T2Y.id = T1.Y
This selects both names as separate columns:
Select T1.somedata,T1.somedata1,T2a.name, T2b.name
from T1
Inner Join T2 as T2a ON T1.X=T2a.id
Inner Join T2 as T2b ON T1.Y=T2b.id
The following would generate two records in the result set:
Select T1.somedata, T1.somedata1, T2.name
from T1
Inner Join T2 ON T1.X=T2.id Or T1.Y=T2.id
you need to join table T2 twice and supply aliases on the names to avoid ambiguity.
SELECT a.*,
b.name as NameB,
c.name as NameC
FROM T1 a
INNER JOIN T2 b
ON a.x = b.id
INNER JOIN T2 c
On a.y = c.id
You need to join table T2 twice and supply aliases on the names to avoid ambiguity.
SELECT a.*,
b.name as NameB,
c.name as NameC
FROM T1 a
INNER JOIN T2 b
ON a.x = b.id
INNER JOIN T2 c
On a.y = c.id