简体   繁体   中英

MySQL table/query design - multiples of one item, many comments

I have a site which display images, up to 30 per page.

Users can comment on the images and these comments, or at least the first few, appear under the image if there are comments.

I have a table of image references linked to a folder on my server.

eg

image_id // image id
user_id // user who added
image_url // ref to image

Then a separate table for all comments

comment_id
image_id // link to images table
comm_poster_id // id of user who posted comment

Now, the question is what the best way to call the information together? Ideally in one select

I can't really ajax call under each image as that would be 30 db calls per page which would kill it so whats the alternative/best method?

To clarify, in the select there would only ever be 1 image but there could of course be multiple comments for an image

Hope i've given enough info

EDIT To clarify, the question is what is the best way to collate all this information together for display - can I run one query which pulls all the images in on the page also somehow pulls the comments for images in if they exist.

As for how I would like the data to look... I don't know. This is the first time I've done anything like this so guidance needed if possible.

Ok, well I'm not a php expert, but I got you started on the sql side of things. I CAN help you with php, but there are others here that are more versed in it that I am.

I started this sqlFiddle for you, go have a look and you can tinker with the query to get what you want.

http://sqlfiddle.com/#!2/79ecf/1/0

From the php side, until you know how you want to display your data, it's difficult to say what your query needs to look like. I went with this for the time being:

select *
from images i
inner join comments c on i.image_id=c.image_id;

This is a VERY simple query and you will probably end up needing to add to it.

I'll assume you are using mysql as most people using php choose mysql. From my understanding there are 2 ways to connect, mysqli and pdo. PDO seems to be emerging as the preferred method, but I know nothing about it. Here are references for both. Just DO NOT USE mysql_query(), it is deprecated so don't bother learning any part of it.

PDO: http://dev.mysql.com/doc/refman/5.6/en/apis-php-pdo-mysql.html
MYSQLI: http://php.net/manual/en/mysqli.query.php

Either of these should give enough of a tutorial to show you how to query your database and then loop through the results to get to your data. It is then up to you how you want to display it on your page.

Hopefully this is enough to point you in the right direction.

Good Luck!

Your easiest approach is to just join the two tables together, sorting first by image and sub-sorting by comment.

SELECT i.*, c.comment_id, c.comm_poster_id 
FROM images i LEFT JOIN comments c ON i.image_id=c.image_id
WHERE {whatever where clause selects your set of 30 images}
ORDER BY i.image_id, c.comm_poster_id

Use a LEFT JOIN or images without comments won't display.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM