1

I have 3 tables: photos, users, votes. I want to get all photos with usernames and votes:

SELECT
    `f`.`Photo`,
    `u`.`Name`,
            `u`.`Surname`,
    COUNT(`h`.`Id`) AS `Votes`
    FROM
        `photos` `f`
    JOIN
        `users` `u`
    ON
        `f`.`UserId` = `u`.`Id`
    LEFT JOIN
        `votes` `h`
    ON
        `f`.`Id` = `h`.`PhotoId`
    WHERE
        `f`.`Show` = '1'
    GROUP BY 
        `h`.`PhotoId`

Data:

photos:
---------------------------
|Id  | Photo     | UserId |
---------------------------
|1   | pic1.jpg  | 1      |
---------------------------
|2   | pic2.jpg  | 2      |
---------------------------
|3   | pic3.jpg  | 3      |
---------------------------

users:
---------------------------
|Id  | Name      | Surname|
---------------------------
|1   | User1     | Sur1   |
---------------------------
|2   | User2     | Sur2   |
---------------------------
|3   | User3     | Sur3   |
---------------------------

votes (is empty):
---------------------------
|Id  | PhotoId   | Date   |
---------------------------

The query above will return only one row, when there are no votes:


Photo     | Name   | Surname | Votes
-------------------------------------
pic1.jpg  | User1  | Sur1    | 0
-------------------------------------

but I'd like to get all three rows (for all three photos):

-------------------------------------
Photo     | Name   | Surname | Votes
-------------------------------------
pic1.jpg  | User1  | Sur1    | 0
-------------------------------------
pic2.jpg  | User2  | Sur2    | 0
-------------------------------------
pic3.jpg  | User3  | Sur3    | 0
-------------------------------------

SOLVED: The GROUP BY clause should be f.Id, not h.PhotoId:

SELECT
`f`.`Photo`,
`u`.`Name`,
        `u`.`Surname`,
COUNT(`h`.`Id`) AS `Votes`
FROM
    `photos` `f`
JOIN
    `users` `u`
ON
    `f`.`UserId` = `u`.`Id`
LEFT JOIN
    `votes` `h`
ON
    `f`.`Id` = `h`.`PhotoId`
WHERE
    `f`.`Show` = '1'
GROUP BY 
    `f`.`Id`
4
  • 1
    Is the f.Show in there significant? Commented Sep 12, 2011 at 6:28
  • 1
    To amplify on @Ray's comment, if f.show is 1 for only one photo row, then you'll get only one row back. Commented Sep 12, 2011 at 6:35
  • Thanks for response guys, but I've made a mistake in GROUP BY clause. Commented Sep 12, 2011 at 6:42
  • @Peter, you are expected to tag an answer as "Accepted" rather than editing the question title ;-) Commented Sep 12, 2011 at 6:45

1 Answer 1

1

You shouldn't be grouping by h.PhotoId. If your votes table is empty, this column will be NULL all the way down. But when you LEFT JOIN on the votes table, it creates one row for each vote with duplicate details for all the rows in the non-votes tables. So you can group on any unique column from another table. f.Id is a good choice. So change your last line to:

GROUP BY
    `f`.`Id`
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.