• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 248
  • Last Modified:

Left outer join - where

I am using LEFT OUTER JOIN to Table2. If t2.CId is not =@CId I do not want to return that row.
How is best to do that?

Table1:
T1Id   T1Text
1          One
2          Two

Table2
T2Id    T1Id   CId   T2Text
1          2       1        Eins
2          2       2        Zwei

@C=1

Result should be:

T1Id  T1Text    T2Id      CId       T2Text
1        One        NULL   NULL    NULL
2        Two        1           1            Eins
 
It shold not return:
2        Two        2          2            Zwei
0
johnkainn
Asked:
johnkainn
  • 2
  • 2
1 Solution
 
tigin44Commented:
try this
SELECT T1Id, T1Text, T2Id, CId, T2Text
FROM Table1 T1
		LEFT OUTER JOIN Table2 T2 ON T1.T1Id = T2.T2Id
WHERE T2.CId = 1

Open in new window

0
 
LimbeckCommented:
SELECT T1Id, T1Text, T2Id, CId, T2Text
FROM Table1 T1
                LEFT OUTER JOIN Table2 T2 ON T1.T1Id = T2.T2Id AND T2.CId = 1
0
 
tigin44Commented:
my query gives exactly what you are looking for. Why did you choose the other solution?
0
 
LimbeckCommented:
because your where clause overrules your left join :) give it a try in a testdatabae
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now