2

I have an image website, where users can upload images into public groups with basic functionality such as likes etc. I am currently building a statistics page for said groups and ideally need a single query that can get all of the likes from all of the images in a group.

Tables(with sample data below)

Group_images: ID | GROUP_ID | IMAGE_ID | TIMESTAMP
               1 |    1     |    10    | 1346256053


Likes: ID | USER_ID | IMAGE_ID | TIMESTAMP
        1 |    1    |   10     | 1346256090

Images : ID | HEADING | UPLOADER | FILENAME | EXT | TIMESTAMP
         10 | Cats    |    4     | 82375dss | .png| 134625400

So in theory it should do something like this,

Search for any images in group_images where group_id = '$id' --> gather all the images that are linked to the group --> search likes for all likes that have been associated with the group by searching for any that have a matching image_id from the first query.

I hope i have made this clear, if not please feel free to ask more questions and i will update the question with further details you need. Thank you.

EDIT I have added sample data below the tables, what i would like to achieve is to be able to see all the user_id's from all the likes from group id = 1.

4

2 に答える 2

0

JOIN集約と呼ばれる非常に基本的な SQL の概念を使用する 必要があります ( COUNTGROUP BY、および現在必要のない他の多くの概念)。

SELECT
   HEADING, UPLOADER, FILENAME, EXT, COUNT(Likes.*)
FROM
   Group_Images gi
   JOIN Images i ON (gi.IMAGE_ID = i.ID)
   JOIN Likes l ON (i.ID = l.IMAGE_ID)
WHERE
   gi.ID = ?
GROUP BY
   i.ID
于 2012-08-29T16:05:36.783 に答える
0

単純なサブクエリでこれを行うことができました。ご回答ありがとうございます。

于 2012-08-29T18:02:48.100 に答える