Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4036380
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:12:57+00:00 2026-05-20T12:12:57+00:00

I have quite a bit of knowledge about SQL queries. I’m trying to make

  • 0

I have quite a bit of knowledge about SQL queries.

I’m trying to make gallery, and I need to select categories from table “cat_photos”, which contain rows (id,name,cover,photo) and count number of photos from table “photos” which contain rows (id,thumb,photo,category).

Here is code which i use:

1) Selecting categories

$query = mysql_query("SELECT * FROM cat_photos ORDER BY ID DESC");
               while($data = mysql_fetch_array($query)) {       
        echo "<li><a href='photos.php?cat=$data[id]'><img src='galleries/categories/$row[image]' alt='$row[name]' /></a>
              <div class='photodesc'><div class='catname'><a href='photos.php?cat=$row[id]'>$row[name]</a></div>
              <div class='catcount'>Number of photos in category</div></div></li>"; }

2) Counting number of photos in category

$query = mysql_query("SELECT category, COUNT(photo) FROM photos GROUP BY category") or die(mysql_error());
               while($row = mysql_fetch_array($query)){
        echo "Number of photos is ". $row['COUNT(photo)'] ." in cateogry ". $row['category'] .".";
        echo "<br />"; }

Separated all works, but I can’t find a way to merge them into one query.
I have googleing for “UNION”, “JOIN”, “LEFT JOIN” options in MySql query but I could’t together the pieces.

I wonder if this is in general possible?
How in order that query look like?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T12:12:58+00:00Added an answer on May 20, 2026 at 12:12 pm

    Try this, it should work :

    SELECT cat_photos.*, count(photos.id) as number_photos
    FROM cat_photos
    LEFT JOIN photos ON photos.category = cat_photos.id
    GROUP BY cat_photos.id, cat_photos.name, cat_photos.image
    ORDER BY cat_photos.id
    

    The number of photos will be accessible trough $row['number_photos'].

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been thinking quite a bit here lately about screen scraping and what
I have a command-line utility that gets quite a bit of downloads from my
I have (after quite a bit of work) a web test that will upload
I have a Windows Form that takes quite a bit of time to load
I have a page which does quite a bit of work and I don't
I've done quite a bit of programming on Windows but now I have to
I've been using extension methods quite a bit recently and have found a lot
I have worked a bit with Django and I quite like its project/applications model
I have quite a large list of words in a txt file and I'm
I have quite a few RewriteRules in my .htaccess that looks like this RewriteRule

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.