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 3791998
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:33:18+00:00 2026-05-19T12:33:18+00:00

I’m no good at writing MySQL queries, so this query isn’t working as it

  • 0

I’m no good at writing MySQL queries, so this query isn’t working as it should. I want to select all “group”s, and fetch external information on the groups by IDs saved in group, like the group’s creator, last person to update it and the number of images in the group.

In this query I’m using INNER JOIN, but apparently groups that have no images arent selected in this query. I want the query to select ALL groups no matter what, and fetch information on all the groups using the IDs. If a group has no images, i want the image count to be 0, not the group to be ignored.

This is the current query:

SELECT g.id, g.name, g.date_created, g.date_updated, g.created_by, 
    c.fullname AS creator_name, g.updated_by, u.fullname AS updater_name, COUNT(i.id) as image_count

FROM    gallery_groups g INNER JOIN 
        users c INNER JOIN 
        users u INNER JOIN 
        gallery_images i

WHERE g.created_by=c.id AND g.updated_by=u.id AND i.group=g.id $id
GROUP BY g.name
ORDER BY g.date_updated DESC, g.name

I have tried replacing INNER JOIN with LEFT JOIN, RIGHT JOIN and OUTER JOIN, but all just result in sql errors.

Thanks for any and all help!

  • 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-19T12:33:18+00:00Added an answer on May 19, 2026 at 12:33 pm

    Put the join criteria with the joins where they belong.
    Don’t mix aggregates (sum/max etc on columns) and non-aggregates (fields not in the GROUP BY and not in a function) even if MySQL allows you to

    SELECT g.id, g.name, g.date_created, g.date_updated, g.created_by, 
        c.fullname AS creator_name, g.updated_by, u.fullname AS updater_name, COUNT(i.id) as image_count
    FROM    gallery_groups g LEFT JOIN 
            users c ON g.created_by=c.id LEFT JOIN 
            users u ON g.updated_by=u.id LEFT JOIN
            gallery_images i ON i.group=g.id
    WHERE g.id = $id
    GROUP BY g.id, g.name, g.date_created, g.date_updated, g.created_by, 
        c.fullname, g.updated_by, u.fullname
    ORDER BY g.date_updated DESC, g.name
    

    Since you are grouping by pretty much everything else except the image count, it may be better to just subquery that

    SELECT g.id, g.name, g.date_created, g.date_updated, g.created_by, 
        c.fullname AS creator_name, g.updated_by, u.fullname AS updater_name,
        IFNULL((
            select COUNT(1)
            from gallery_images i
            WHERE i.group=g.id), 0) as image_count
    FROM    gallery_groups g LEFT JOIN 
            users c ON g.created_by=c.id LEFT JOIN 
            users u ON g.updated_by=u.id LEFT JOIN
            gallery_images i ON i.group=g.id
    WHERE g.id = $id
    ORDER BY g.date_updated DESC, g.name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from

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.