I have three tables I wish to inner join by a common column between them.
Say my tables are;
TableA TableB TableC
I wish to join A-B, but then also B-C all by this common field I will call common.
I have joined two tables like this;
dbo.tableA AS A INNER JOIN dbo.TableB AS B
ON A.common = B.common
How do I add the third one?
select *
from
tableA a
inner join
tableB b
on a.common = b.common
inner join
TableC c
on b.common = c.common
Just do the same thing agin but then for TableC
SELECT *
FROM dbo.tableA A
INNER JOIN dbo.TableB B ON A.common = B.common
INNER JOIN dbo.TableC C ON A.common = C.common
dbo.tableA AS A INNER JOIN dbo.TableB AS B
ON A.common = B.common INNER JOIN TableC C
ON B.common = C.common
try the following code
select * from TableA A
inner join TableB B on A.Column=B.Column
inner join TableC C on A.Column=C.Column
try this:
SELECT * FROM TableA
JOIN TableB ON TableA.primary_key = TableB.foreign_key
JOIN TableB ON TableB.foreign_key = TableC.foreign_key
Related
I want join 6 tables:
Table A join Table B
Table A join Table C
Table A Join Table D
Table D Join Table E
Table E Join Table F
But I can't run it:
SELECT
A.ID, A.Name, A.SName,
B.tel, B.status,
C.ComCode, C.Comdes,
D.Type,
E.Des, E.AreaDes,
F.AreaID
FROM
((((Table A
INNER JOIN
Table B ON A.ID = B.ID)
INNER JOIN
Table C ON A.Concode = C.Comcode)
INNER JOIN
Table D ON A.Type = D.Type)
INNER JOIN
Table E ON D.Des = E.Des)
INNER JOIN
Table F ON E.AreaID = F.AreaID
Get rid of your parentheses and table and it should be vailid SQL, so
SELECT ...
FROM a INNER JOIN b ON a.a = b.a
INNER JOIN c ON b.b = c.a
...
Try this :
SELECT
A.ID, A.Name, A.SName,
B.tel, B.status,
C.ComCode, C.Comdes,
D.Type,
E.Des, E.AreaDes,
F.AreaID
FROM Table A
INNER JOIN Table B ON A.ID = B.ID
INNER JOIN Table C ON A.Concode = C.Comcode
INNER JOIN Table D ON A.Type = D.Type
INNER JOIN Table E ON D.Des = E.Des
INNER JOIN Table F ON E.AreaID = F.AreaID
It will help if you tell us what you database server is e.g., SQL Server, Oracle, MySQL, etc. When you say, "...I can't run it..." are you getting an error? If so, can you share that.
I ditched your parenthesis...
SELECT
A.ID,
A.Name,
A.SName,
B.tel,
B.status,
C.ComCode,
C.Comdes,
D.Type,
E.Des,
E.AreaDes,
F.AreaID
FROM
TableA A INNER JOIN TableB B ON A.ID = B.ID
INNER JOIN TableC C ON A.Concode = C.Comcode
INNER JOIN TableD D ON A.Type = D.Type
INNER JOIN TableE E ON D.Des = E.Des
INNER JOIN TableF F ON E.AreaID = F.AreaID
For clarity, I adjusted your table names (so it was clear what was Table A vs. Table B).
I have two tables Say A and B. A is master table and B is child table, from which I need values as below.
select A.Id, A.Name, B.Path from A,B where A.Id=B.Id
Now, I want to add column of 3rd table which is child of table 'B', say C i.e. C.File.
The value of C.File will be null if C.SubId=B.SubId is false else will return value when condition becomes true.
This is the exact definition of a left join:
SELECT a.id, b.name, b.path, c.file
FROM a
JOIN b ON a.id = b.id
LEFT JOIN c ON b.subid = c.subid
You need to LEFT JOIN your third table from what I can gather.
SELECT A.Id, A.Name, B.Path, C.file
FROM tableA a
INNER JOIN tableB b ON a.id = b.id
LEFT JOIN tableC c ON b.subid = c.subid
Simply Join all the three tables using INNER JOIN
select A.Id, A.Name, B.Path ,C.File
FROM A
INNER JOIN B
ON A.Id=B.Id
INNER JOIN C
ON C.SubId=B.SubId
table A(a_id)
table B (b_id)
table c(c_id , b_id, a_id);
select a.*, b.* , c*
from c join b
i'm confused from here ???
Try this -
SELECT * FROM A
INNER JOIN C
ON A.a_id = C.a_id
INNER JOIN B
ON B.b_id = C.b_id;
SELECT a.*, b.*, c*
FROM c
LEFT JOIN b USING (b_id)
LEFT JOIN a USING (a_id);
Try this
SELECT A.* from tableA A
LEFT JOIN tableC C ON C.a_id = A.a_id
LEFT JOIN tableB B ON C.b_id = B.b_id
You could use INNER, LEFT, RIGHT, FULL... JOINS in following:
SELECT *
FROM A
LEFT JOIN B ON A.a_id = B.b_id
LEFT JOIN C ON B.b_id = C.c_id
I would like to do something like this but can't get it to work:
SELECT A.*,B.*,C.* FROM tableA A LEFT JOIN
B ON A.ID = B.ID
C ON A.ID = C.ID
I.e. I need to use a field from the first table for all joins as tableB and tableC don't
has any fields to link them together.
Can this be done?
you missed a join !
SELECT A.*,B.*,C.* FROM tableA A
LEFT JOIN
B ON A.ID = B.ID
left join
C ON A.ID = C.ID
Try this one -
SELECT *
FROM dbo.tableA A
LEFT JOIN dbo.B ON A.ID = B.ID
LEFT JOIN dbo.C ON A.ID = C.ID
I have the following tables:
TableA (id, tableB_id, tableC_id)
TableB (id, expirationDate)
TableC (id, expirationDate)
I want to retrieve all the results from TableA ordered by tableB.expirationDate and tableC.expirationDate. How can I do this?
select ta.*
from TableA ta
inner join TableB tb on ta.tableB_id = tb.id
inner join TableC tc on ta.tableC_id = tc.id
order by tb.expirationDate, tc.expirationDate
Update:
If you are not getting all the records, then you'll need to use a left outer join:
select ta.*
from TableA ta
left outer join TableB tb on ta.tableB_id = tb.id
left outer join TableC tc on ta.tableC_id = tc.id
order by tb.expirationDate, tc.expirationDate
If the result set is empty with the other suggestions, are you sure the data in the tables is actually correctly correlated to each other?
Can you post some sample rows for each table?
Have you tried:
SELECT a.* FROM TableA a
INNER JOIN TableB b on b.id = a.tableB_id
INNER JOIN TableC c on c.id = a.tableC_id
ORDER BY b.expirationDate, c.expirationDatetableB_id