Connecting Tech Pros Worldwide Forums | Help | Site Map

count field in database only returning values > 1

Member
 
Join Date: May 2007
Posts: 36
#1: Jul 17 '07
Hello
I have the following query

Expand|Select|Wrap|Line Numbers
  1. SELECT COUNT(ur.resourceId) AS times_downloaded
  2. FROM Resource as r, user_resource as ur
  3. WHERE r.resourceId = ur.resourceId
  4. Group BY ur.resourceId
  5.  
The User_Resource table indicates how many times a user has downloaded a resource. If the resource has not yet been downloaded they will not appear in my query because of the ?
Expand|Select|Wrap|Line Numbers
  1. WHERE r.resourceId = ur.resourceId
  2.  
Does anyone know how to fix this?
If the resource has not yet been downloaded it should still appear with 0 times downloaded

mwasif's Avatar
Moderator
 
Join Date: Jul 2006
Location: Pakistan
Posts: 719
#2: Jul 17 '07

re: count field in database only returning values > 1


Use LEFT JOIN

Expand|Select|Wrap|Line Numbers
  1. SELECT COUNT(ur.resourceId) AS times_downloaded
  2. FROM Resource AS r 
  3. LEFT JOIN user_resource AS ur 
  4. ON r.resourceId = ur.resourceId
  5. Group BY ur.resourceId
Member
 
Join Date: May 2007
Posts: 36
#3: Jul 17 '07

re: count field in database only returning values > 1


Quote:

Originally Posted by mwasif

Use LEFT JOIN

Expand|Select|Wrap|Line Numbers
  1. SELECT COUNT(ur.resourceId) AS times_downloaded
  2. FROM Resource AS r 
  3. LEFT JOIN user_resource AS ur 
  4. ON r.resourceId = ur.resourceId
  5. Group BY ur.resourceId

could you please explain what this does in this case?
as opposed to right join and inner join?

Thanks
Member
 
Join Date: May 2007
Posts: 36
#4: Jul 18 '07

re: count field in database only returning values > 1


Quote:

Originally Posted by mwasif

Use LEFT JOIN

Expand|Select|Wrap|Line Numbers
  1. SELECT COUNT(ur.resourceId) AS times_downloaded
  2. FROM Resource AS r 
  3. LEFT JOIN user_resource AS ur 
  4. ON r.resourceId = ur.resourceId
  5. Group BY ur.resourceId

Hi
this did not work in my case. it I have 6 empty fields and it only returns one of them....

Table User_Resource
UserResourceId int(11)
ResourceId int(11)

Table Resource
ResourceId int(11)


Table Resource contains 15 resources and only nine of them have been downloaded. But I only see 10 of them in my list. Plz help
Member
 
Join Date: May 2007
Posts: 36
#5: Jul 18 '07

re: count field in database only returning values > 1


Quote:

Originally Posted by mwasif

Use LEFT JOIN

Expand|Select|Wrap|Line Numbers
  1. SELECT COUNT(ur.resourceId) AS times_downloaded
  2. FROM Resource AS r 
  3. LEFT JOIN user_resource AS ur 
  4. ON r.resourceId = ur.resourceId
  5. Group BY ur.resourceId


I got it to work. I had to take group by r.resourceId and not ur.resourceId

thanks alot
Reply