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

  • SEARCH
  • Home
  • 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

We have quite a bit of reusable code at work (a good thing). Whenever
I'm making an engine/CMS for story-based web browser games. I have quite a bit
My one activity seems to have grown quite a bit and I feel like
Okay I have updated my code quite a bit. I am getting a new
I have a pre tag within my page that contains quite a bit of
I've done quite a bit of googling on this error but have had no
I've read around quite a bit but haven't found a definitive answer. I have
I'm quite new to MediaWiki, and now I have a bit of a problem.
I have quite big document in html format that generated from Microsoft Word. It
I'm building a few small prototypes in Silverlight and have quite a bit of

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.