INNER and LEFT OUTER join help

Let's say I have 3 tables. Table A, TableB, TableC

I need to manage the recordset available after an INNER JOIN.

Set 1 -> TableA INNER JOIN TableB
Set 2 -> TableC INNER JOIN TableB

      

I need set 1, whether empty or not (2) if not.

Basically I am trying to write a query and have come this far

SELECT *
  FROM TableA 
       INNER JOIN TableB ON ...
       LEFT OUTER JOIN (TableC INNER JOIN TableB)

      

How do I write in SQL Server?

EDIT: I'm actually trying to concatenate multiple tables. How would your answer change if I need to join multiple ex tables: OUTER JOIN OF (INNER JOIN of TableA and TableB) and (INNER JOIN TableC and TableD) NOTE. In the equation

there is a new DDD table.
+1


a source to share


9 replies


 SELECT * FROM TableA 
       INNER JOIN TableB ON TableB.id = TableA.id
       LEFT JOIN TABLEC ON TABLEC.id = TABLEB.id

      

I don't know what columns you are trying to use, but it is just that simple

Edit: Looking at your edit, it seems like you are confused about what Joins is actually doing. In the above example, you will get the following results.

Columns -> You will get all columns for TableA, TableB and TableC



Rows-> You will start with all rows from Table A. Then you will delete all rows from Table A that do not have a corresponding "id" in Table B. (You will have duplicates if it is not a 1: 1 ratio between TableA and TableB) ...

Now, if you take the results from the top, you will be matching any records from Table C that match the TableB.id column. Any rows from above that do not have a matching TableC record will get null for all columns from TableC in the results.

ADVICE. I'm sure only part of this makes sense to you, but my advice is that you start writing some queries, predict the results, and then see if your predictions are correct to figure out what you are doing.

+4


a source


What you want is not a JOIN, but a UNION.



SELECT * FROM TableA INNER JOIN TableB ON ...
UNION
SELECT * FROM TableC INNER JOIN TableD ON ...

      

+2


a source


In fact, you can add ordering to your connections, like in a math equation where you can do this: (5 + 4) * (3 + 1).

Considering the second part of your question, try:

SELECT
     <your columns>
FROM
     (TableA INNER JOIN Table B ON <join criteria for A to B>)
LEFT OUTER JOIN
     (TableC INNER JOIN Table D ON <join criteria for C to D>) ON
     <join criteria for AxB to CxD>

      

+2


a source


Select * from ((((TableA a inner join TableB b on a.id = b.id) 
                left outer join TableC c on b.id = c.id)
                full outer join TableD d on c.id = d.id)
                right outer join TableE e on e.id = d.id)
                /* etc, etc... */

      

You can loose the parentheses if you like.

+1


a source


try it.

SELECT *
  FROM TableA              a 
       INNER JOIN TableB   b ON a.id=b.id
       LEFT OUTER JOIN (SELECT *
                            FROM TableC            c
                                INNER JOIN TableD  d on c.id=d.id
                       ) dt on b.id=dt.id

      

0


a source


Assuming I understand your question, I think this is what you are asking for:

SELECT * 
FROM TableA INNER JOIN TableB on TableA.JoinColumn = TableB.JoinColumn
LEFT OUTER JOIN TableC on TableB.JoinColum = TableC.JoinColumn
INNER JOIN TableD on TableC.JoinColumn = TableD.JoinColumn

      

Note that the JoinColumn used to join A and B does not have to be the same column as the one used to join B and C, and so on for C and D.

0


a source


You haven't specified join conditions or explained how tables should be linked, so it's not clear how this can be simplified.

SELECT a.a_id, b1.b_id b1_id, b2_id, bc.c_id
FROM TableA a JOIN TableB b1 on a.b_id = b1.b_id
LEFT JOIN (SELECT c.c_id, b2.b_id b2_id
    FROM TableC c JOIN TableB b2 ON c.b_id = b2.b_id
  ) bc ON bc.c_id = a.c_id;

      

Looking at your last edit, you can do something like:

SELECT <columns>
FROM (SELECT <columns> FROM TableA JOIN TableB ON <A-B join conditions>)
           LEFT JOIN
           (SELECT <columns> FROM TableC JOIN TableD ON <C-D join conditions>)
           ON <AB-CD join conditions>

      

Although you really don't need internal projections and can do:

SELECT <columns>
FROM (TableA a JOIN TableB b ON <A-B join conditions>)
           LEFT JOIN
           (TableC c JOIN TableD d ON <C-D join conditions>)
           ON <AB-CD join conditions>

      

If AB-CD join conditions are written in terms of columns a, b, c, d, etc. directly.

0


a source


Since you are using Sql Server, why not create views to help you? Filling everything in a giant Sql statement can get hard to read. An approximate view might look like this:

create view AandB
as
select *
from A
inner join B on B.aid = A.aid

      

And the same goes for CandD. Then you can get an optional connection with plain Sql:

select *
from AndB
left outer join CandD on AndB.cid = CandD.cid

      

If you are interested in strings from both sets, you can do a full join:

select *
from AndB
full outer join CandD on AndB.cid = CandD.cid

      

0


a source


SELECT *
  FROM TableA A
 INNER JOIN TableB B  ON B.?? = A.??  AND ...
  LEFT JOIN TableC C  ON C.?? = B.??  AND ...
  LEFT JOIN TableB B2 ON B2.?? = C.?? AND ...
  LEFT JOIN TableD D  ON D.?? = C.??  AND ...

      

So here's the thing: Logically, joins are not actually between specific tables, they are between the table and the rest of the "set" (joins and tables). Therefore, as long as you know that there is a 1 to 1 relationship between C and B2, or between C and D, you cannot INNER JOIN on C, because C can be empty from it. LEFT JOIN to B which will eliminate those lines, effectively destroying your LEFT join.

Thus, in any case, any joins to the table to which LEFT is bound must also be bound to LEFT external. It makes sense?

0


a source







All Articles