Jump to content

Query help


ToonMariner

Recommended Posts

Hi everyone,

 

Having  a spot of bother with a join query...

 

I have 3 tables media, user and media votes and one of the parameters i need to return is the number of votes an item in teh media table has - this is determined by the number of records in teh media_vote table.

 

I have the following query...

SELECT
`media`.*	,
`user`.`username`	,
DATEDIFF(NOW(), `media`.`added`)	AS `period`	,
IF (`media`.`rating` > 0, COUNT(`media_votes`.`vote_id`), 0)		AS `vote_no`
FROM
`media`	,
`user`	,
`media_votes`
WHERE
`media`.`flag`	!=	'm'
AND
`user`.`user_id`	=	`media`.`user_id`
AND
`media`.`categories` NOT IN (21, 40)
AND
`user`.`active`	=	'y'
AND
`media_votes`.`media_id`	=	`media`.`media_id`
GROUP BY
`media_votes`.`media_id`
ORDER BY
`media`.`added` DESC

 

all is dandy EXCEPT when teh item has no votes in the media_vote table.

 

IF (`media`.`rating` > 0, COUNT(`media_votes`.`vote_id`), 0) AS `vote_no`

 

was my feeble attempt to get round this but its clearly the

 

`media_votes`.`media_id` = `media`.`media_id`

 

portion which is giving me the headache and I just can't think how to get round it.

 

Any help would be very much appreciated...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.