Jump to content

[SOLVED] Query Question


therealwesfoster

Recommended Posts

Ok, situation 1: I have 2 tables, 1 is "comments" and the other is "images". Users comment the images etc.

 

Well on one page I'm wanting to sort the images by their number of comments. How would I create an SQL query to do this? The easy way would be to put a "total_comments" field in the "images" table, but I'll do that as a last resort.

 

Situation 2

I'm wanting to sort the images by their rating (which is figured by dividing "pic_rating" / "pic_votes"). How can I do that simple math in an SQL query in order to get the top rated pics to show?

 

 

Thanks :)

Link to comment
Share on other sites

first you need to count the number of comments per image:

 

Assuming that each comment has a reference back to the image table (imageID)

 

Select imageID, count(imageID) as number from Comments group by imageID

 

Creates a table consisting of the image ID and number of comments with that ID.

 

Now join it to the comments table:

 

Select
images.*,
commentInfo.*
from 
(Select imageID, count(imageID) as number from Comments group by imageID) as commentInfo
join images on commentinfo.imageID = images.index
order by commentInfo.number

 

Sit 2

 


Select pic_rating / pic_votes as rating from images order by rating

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.