|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Help with SELECT statementHi,
I have two tables with a UserID column and need to construct a query that lists all UserIDs from Table A that is not present in Table B. Any help with this select statement would be appreciated Niclas Select A.* from TableA as A where Not Exists (select * from TableB as B
where B.UserId = A.UserId) You can also go with a Left Outer Join but it's a little more complicated to understand: Select A.* from TableA as A Left Outer Join TableB as B on A.UserId = B.UserId Where B.UserId is Null Show quote "Niclas" <lindblom_nic***@hotmail.com> wrote in message news:eipps755FHA.4076@tk2msftngp13.phx.gbl... > Hi, > > I have two tables with a UserID column and need to construct a query that > lists all UserIDs from Table A that is not present in Table B. > > Any help with this select statement would be appreciated > > Niclas > A third possibility would be to use the IN clause:
Select A.* from TableA as A where A.UserId Not IN (select UserId from TableB Where UserId is not Null) The condition « Where B.UserId is Not Null » is a necessity if there is a possibility that B.UserId can be Null; otherwise the result won't be good if the IN clause encounter a Null value. Show quote "Niclas" <lindblom_nic***@hotmail.com> wrote in message news:eipps755FHA.4076@tk2msftngp13.phx.gbl... > Hi, > > I have two tables with a UserID column and need to construct a query that > lists all UserIDs from Table A that is not present in Table B. > > Any help with this select statement would be appreciated > > Niclas > Many thanks !
Niclas Show quote "Sylvain Lafontaine" <sylvain aei ca (fill the blanks, no spam please)> wrote in message news:emU3YG65FHA.2888@tk2msftngp13.phx.gbl... >A third possibility would be to use the IN clause: > > Select A.* from TableA as A where A.UserId Not IN (select UserId from > TableB Where UserId is not Null) > > The condition « Where B.UserId is Not Null » is a necessity if there is a > possibility that B.UserId can be Null; otherwise the result won't be good > if the IN clause encounter a Null value. > > -- > Sylvain Lafontaine, ing. > MVP - Technologies Virtual-PC > E-mail: http://cerbermail.com/?QugbLEWINF > > > "Niclas" <lindblom_nic***@hotmail.com> wrote in message > news:eipps755FHA.4076@tk2msftngp13.phx.gbl... >> Hi, >> >> I have two tables with a UserID column and need to construct a query that >> lists all UserIDs from Table A that is not present in Table B. >> >> Any help with this select statement would be appreciated >> >> Niclas >> > > |
|||||||||||||||||||||||