Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Left Join and Null values 1

Status
Not open for further replies.

ingernet

Programmer
Feb 5, 2001
68
US
Hello,

I'm finally losing what's left of my mind, all thanks to what should be simple query (which is probably breaking all the rules of MySQL).

Here's the scoop: got a simple left join query:

Code:
select users.uId, user_activity.ua_uId 
  from users left join user_activity
  on users.uId = user_activity.ua_uId 
  where user_activity.ua_uId = NULL;

Essentially, I'm looking for all the users who DIDN'T have anything in the user_activity table.

I thought about doing something with a separate query to get everyone who *did* have an activity recorded, and then run a "not in" command against that, but that just seems messy.

Any ideas?

Thanks!
Inger
 
what's the question?

your left join for all the users who DIDN'T have anything in the user_activity table looks okay -- did you run it? what did it produce?

the other query you mentioned is messy


rudy
 
Code:
select users.uId, user_activity.ua_uId
  from users left join user_activity
  on users.uId = user_activity.ua_uId
  where user_activity.ua_uId IS NULL

Nothing is equal or unequal to null.
 
ow

don't i look dumb now, eh

thanks, swampboogie
 
rudy,

i got nothing. 0 records. :(

i suspect that it's because the NULL values are only created as SQL slaps together the results (there aren't any entries in the user_activity table where any of the columns *would* be NULL.

when i do this query -
Code:
select users.uId, user_activity.ua_uId
  from users left join user_activity
  on users.uId = user_activity.ua_uId

- here are my results:

Code:
uId	ua_uId
1	1
3	3
3	3
5	NULL
6	6
6	6
7	NULL

is it possible that the search for null values doesn't work because NULL values didn't exist until they were created ad hoc by SQL as it compiled a left join result? this seems the most logical reason. but i could have sworn that i'd seen other threads where people had done this. (of course, i can't find them NOW....)

thanks,
inger
 
swampBoogie,

thanks for the reminder! that was it.

sigh...semantics, semantics, semantics. :)

thanks to both of you for your help.

inger
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top