Joncom
Joncom

Reputation: 2146

Using COUNT() to find how many rows exist in another table

This is the query that's been getting me everything I wanted to know about photos in my database up until now:

SELECT
      users.facebook_id,
      users.first_name,
      users.last_name,
      photos.*,
      bookmarks.photo_id AS bookmark
FROM
      photos
JOIN users
      ON photos.author = users.facebook_id
LEFT JOIN bookmarks
    ON photos.id = bookmarks.photo_id
    AND bookmarks.facebook_id = 123456789
WHERE photos.description <> ''
ORDER BY photos.id DESC

However now, I'd like to get one more piece of information. I'd like to know how many votes have been cast on a particular photo.

Here is my votes table:

CREATE TABLE IF NOT EXISTS `votes` (
  `photo_id` int(11) NOT NULL,
  `facebook_id` bigint(20) NOT NULL COMMENT 'The user''s Facebook ID.',
  `date` varchar(10) NOT NULL COMMENT 'Date formatted as YYYY-MM-DD.',
  UNIQUE KEY `one_vote_per_day` (`photo_id`,`facebook_id`,`date`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

And here is what I tried already:

SELECT
      count(votes.photo_id) as vote_count,
      users.facebook_id,
      users.first_name,
      users.last_name,
      photos.*,
      bookmarks.photo_id AS bookmark
FROM
      photos
JOIN users
      ON photos.author = users.facebook_id
LEFT JOIN votes
    ON votes.id = photos.photo_id
LEFT JOIN bookmarks
    ON photos.id = bookmarks.photo_id
    AND bookmarks.facebook_id = 123456789
WHERE photos.description <> ''
ORDER BY photos.id DESC

Obviously I'm doing something wrong, because when I tried this, I'd never get more than a single photo in the result.

This method works fine for me when I only need information on a single photo, but this query needs to work for all photos at once...

Upvotes: 1

Views: 1229

Answers (2)

Robert
Robert

Reputation: 25753

You need group by

SELECT
      count(votes.photo_id) as vote_count,
      users.facebook_id,
      users.first_name,
      users.last_name,
      --photos.*, you have to add all fileds you need, and add them to group by
      bookmarks.photo_id AS bookmark
FROM
      photos
JOIN users
      ON photos.author = users.facebook_id
LEFT JOIN votes
    ON votes.id = photos.photo_id
LEFT JOIN bookmarks
    ON photos.id = bookmarks.photo_id
    AND bookmarks.facebook_id = 123456789
WHERE photos.description <> ''
group by users.facebook_id,
         users.first_name,
         users.last_name,
         bookmarks.photo_id
ORDER BY photos.id DESC

Upvotes: 3

npe
npe

Reputation: 15699

Not sure if MySQL supports nested queries in SELECT, but try something like this:

SELECT
      users.facebook_id,
      users.first_name,
      users.last_name,
      photos.*,
      bookmarks.photo_id AS bookmark,
      (select count(*) from votes where votes.photo_id = photo.id) AS number_of_votes
FROM
      photos
JOIN users
      ON photos.author = users.facebook_id
LEFT JOIN bookmarks
    ON photos.id = bookmarks.photo_id
    AND bookmarks.facebook_id = 123456789
WHERE photos.description <> ''
ORDER BY photos.id DESC

Upvotes: 0

Related Questions