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
https://forums.phpfreaks.com/topic/114001-query-help/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.