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 a query which involves a left join followed by a join. I want to make sure the left join is done first. The left join comes before the join nin my query, is this enough? This is how the join looks like
select * from
(select *....) A
left join
(select *...) B
on A.a = B.a
left join
C
on A.f = C.f
I cannot see the JOIN in your code, only two LEFT JOIN statements.
However, if you have something like this:
select * from
(select *....) A
left join
(select *...) B
on A.a = B.a
join
C
on A.f = C.f
and you want to make sure the LEFT JOIN is executed first, you can move this LEFT JOIN to a sub-query:
select *
from (
select * from (
(select *....) A
left join
(select *...) B
on A.a = B.a
)
) D
join
C
on D.f = C.f
I am having some trouble trying to add 2 more joins to a select. The bellow works for me:
FROM
TABLE1 A
INNER JOIN TABLE2 B ON A.ID = B.ID
LEFT JOIN TABLE3 C ON A.REQUESTED_BY = C.USER_NAME
LEFT JOIN TABLE3 D ON A.COORDINATOR = D.USER_NAME
INNER JOIN TABLE4 E ON A.ID = E.PARENT_ID
INNER JOIN TABLE5 F ON E.ID = F.ID
But I need to get more information, so I tried something like this (added the last 2 rows):
FROM
TABLE1 A
INNER JOIN TABLE2 B ON A.ID = B.ID
LEFT JOIN TABLE3 C ON A.REQUESTED_BY = C.USER_NAME
LEFT JOIN TABLE3 D ON A.COORDINATOR = D.USER_NAME
INNER JOIN TABLE4 E ON A.ID = E.PARENT_ID
INNER JOIN TABLE5 F ON E.ID = F.ID
INNER JOIN TABLE6 G ON A.ID = B.ID
LEFT JOIN TABLE3 H ON G.COORDINATOR = H.USER_NAME
And this isn't working like it should.
Question: How can I add the last two joins to make the select works? Thanks.
You're not actually joining to TABLE6 (G) anywhere. I would think that this join:
INNER JOIN TABLE6 G ON A.ID = B.ID
should be something like this instead:
INNER JOIN TABLE6 G ON A.ID = G.ID
And as a side note, I hope you're using table aliases that are more meaningful than A, B, C, etc. in your real code. ;-)
From everything I have learned about LEFT OUTER JOIN, the table you want to be nullable should be on the right side of the equals symbol. If this is the case, why do both of these queries return the same result:
SELECT *
FROM employees e
LEFT JOIN cars c ON c.employeeID=e.id AND c.name='Honda City'
WHERE c.id IS NULL
ORDER BY e.id ASC;
SELECT *
FROM employees e
LEFT JOIN cars c ON e.id=c.employeeID AND c.name='Honda City'
WHERE c.id IS NULL
ORDER BY e.id ASC;
Demo: http://sqlfiddle.com/#!15/46d00/2
Q1 uses A LEFT JOIN B ON A.id = B.id
Q2 uses A LEFT JOIN B ON B.id = A.id
You have changed the LHS and RHS of the items being compared in the ON clause, but the LEFT join is talking about which TABLE is on the left.
So to see a difference you would make Q2 use "B LEFT JOIN A ON A.id = B.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