Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 JOIN TABLES

Author  Topic 

cipi20052000
Starting Member

5 Posts

Posted - 2009-12-25 : 09:11:32
Hi everybody! I have a question.... I have a database with 2 tables: Adult and Child. The Adult table has the following columns: id_adult(primary key), name, surname. The Child table has the next columns: id_child(primary key), id_adult(foreign key to Adult table), id_adult2(foreign key to Adult table). Ok, now I want to select from the Adult table all the adults which don't have any children. I've tried something like this:

SELECT a.id_adult,a.name,a.surname,c.id_adult1,c.id_adult2
from Adult as a
left join Child as c
on a.id_adult=c.id_adult1 or a.id_adult = c.id_adult2
where c.id_adult1=null or c.id_adult2=null

And the result doesn't show anything. Where I do wrong?

monty

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-12-25 : 13:35:20
Hi,
in case of null you cannot use the = sign.
Use it like this:
where c.id_adult1 is null or c.id_adult2 is null

and for <> use: is not null


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -