Jump to content

Help with SELECT query needed


2sharp

Recommended Posts

Hi all,

I have two tables in the database for my photographic website:
[a href=\"http://www.sharperstill.com/\" target=\"_blank\"]http://www.sharperstill.com/[/a]
One is Categories and contains three fields: a unique id, category_anme, and category_path
The other is Photos and contains four fields: a unique id, photo_filename, photo_caption, and c_id (which links to category tables id field....

What I want to do, with one SELECT query, is extract the names and paths of all categories (for building a list of links) as well as getting a count of the number of pictures in each category (for building 'smart' next/previous links.

All has been going well except that the count returned is always for the number of categories, not the number of pics in each category.

Have been playing with lots of code lately but follwing will show the direction I am heading in:

SELECT category_name, category_path, photo_filename, photo_caption, COUNT(*) FROM category, photos WHERE category.id = photos.c_id GROUP BY category.id ORDER BY id DESC";

The number of pics shown in each category is correct on the output for the links list, but when I use it to show pic x of xx total it is wrong and does not change between categories...

Jon

Link to comment
Share on other sites

Yeah, the COUNT() is returning the number of rows returned, which is necessarily simply the number of categories. COUNTs can be confusing with GROUP BY clauses; you may have to do a subselect, or write another query.

Does the following return the desired result?

[code]SELECT category.category_name, COUNT(*) FROM category, photos WHERE category.id = photos.c_id GROUP BY category.id;[/code]
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.