0

So my question is this.

I would like to Pull data from table 1. Then after pulling that data pull data from table 2 but also show the nulls.

So right now I have this

Select a.*, b.description from table 1 a
LEFT JOIN table 2 on a.id = b.id
WHERE b.description = "hello"

So I would like for it to Always show the information from table and only show the information from table 2 if the where clause is hit

So look something like

Table 1      Table 2
ID1          NULL
ID2          NULL
ID3          hello
ID4          NULL

But all I get back of course is

Table 1     Table 2
ID3         hello

I'm trying to use LINQ to do it but I'm trying to figure it out on SQL first because I know that more.

I don't know if this made any sense but thanks!

KratosMafia
  • 333
  • 1
  • 16

4 Answers4

1

Just remove your where clause. By meaning of left join all the ids that will be there in table1 with ids matching between table1 & table2 will be the o/p

    Select a.*, b.description from table 1 a
    LEFT JOIN table 2 on a.id = b.id and 
      b.description="hello"
Himanshu
  • 3,830
  • 2
  • 10
  • 29
0

You need to use INNER JOIN for this purpose

Select a.*, b.description from table 1 a
INNER JOIN table 2 on a.id = b.id
WHERE b.description = "hello"

Check this answer for detailed explaination

Derviş Kayımbaşıoğlu
  • 28,492
  • 4
  • 50
  • 72
0

Allow null in where

SELECT a.*, b.description 
FROM table_1 a
LEFT JOIN table_2 b ON a.id = b.id
WHERE b.description IS NULL OR b.description = 'hello'
Serg
  • 22,285
  • 5
  • 21
  • 48
0

Add or b.description is null to the WHERE clause:

Select a.*, b.description 
from table1 a LEFT JOIN table2 b 
on a.id = b.id
WHERE b.description = "hello" or b.description is null
forpas
  • 160,666
  • 10
  • 38
  • 76