It's a bit difficult getting my problem into a short form, so I apologise if the title doesn't make sense.
Anyway, here is the problem:
$query = '
SELECT issues.*, comments.author AS commentauthor, favorites.userid AS favorited FROM issues
LEFT JOIN comments ON comments.issue = issues.id AND comments.when_posted = issues.when_updated
LEFT JOIN favorites ON favorites.ticketid = issues.id AND favorites.userid = \'' . $_SESSION['uid'] . '\'
' . $whereclause . '
ORDER BY issues.when_updated ' . $order;
Don't mind the fact that it's PHP as I am not asking for PHP help.
The query retrieves a bunch of issues
, and what I'm wishing to do is obtain the row count of favorites
that have favorites.ticketid
matching issues.id
. My use of LEFT JOIN favorites
is not to get what I've just mentioned, but instead to obtain whether the client has favourited the issue, thus the part favorites.userid AS favorited
.
I have tried doing the following: (all at once, I'm putting this in bulleted form for readibility)
- duplicating the existing
LEFT JOIN favorites
and removing the user id check from the duplicate - adding
, COUNT(favorites.ticketid) AS favoritescount
to theSELECT
section - adding
AS favorited
to the originalLEFT JOIN
as well as changingfavorites.userid
tofavorited.userid
With that attempt, my query ends up returning only one row.