views:

181

answers:

4

I am trying to run a query that selects values from a table using a WHERE clause , the query only returns the rows where all of the conditions have values, hoe do I go about returning the values that are also null?

     Language Table                            Students Table
 ID    Language                        ID      Student      LanguageID
  1     English                         1        Joe
  2     Spanish                         2        Mike          1

Running a query such as

Select student.ID , Student.Student , Language.language 
FROM 
    Students, Language 
WHERE 
    student.LanguageID = Language.id 

The query only returns a single row for student mike I would like to return all student even if the language setting is null.

+1  A: 

Looks like you need an outer join: http://en.wikipedia.org/wiki/Join_%28SQL%29#Outer_joins

Roy Truelove
+1  A: 

WHERE student.LanguageID = Language.id OR student.LanguageID IS NULL

thomasfedb
+3  A: 

You want something like this:

select
  student.id, student.student, language.language
from
  student left join language
      on student.languageid = language.languageid
Ken Redler
+2  A: 

You should not use the comma syntax for joining tables. Use the ISO syntax of JOIN or in this case LEFT OUTER JOIN (which in pretty much all DBMS can be shortened to Left Join):

Select Student.Id, Student.Student. Language.Language
From Students 
    Left Join Language
        On Language.Id = Student.LanguageId

This will return all students and their language if they have one or null if they do not have one.

Thomas