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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:48:33+00:00 2026-06-05T04:48:33+00:00

I have a little problem with a left join where I want a list

  • 0

I have a little problem with a left join where I want a list of designs and on each design I want to display how many comments each design have.

I am using a LEFT JOIN

SELECT ds.*, count(com.comment) AS countcom FROM tdic_designs ds 
LEFT JOIN tdic_comments com ON (com.design_id = ds.id) 
WHERE ds.approved = 1 AND ds.hidden = 0 AND com.approved = 1 
GROUP BY ds.id 
ORDER BY ds.date_added ASC

But that doesn’t work as it only displays one design which have 1 comment, but I have two designs in the table, where the second design doesn’t have a comment.

If I change the SQL to

SELECT ds.*, count(com.comment) AS countcom FROM tdic_designs ds 
LEFT JOIN tdic_comments com ON (com.design_id = ds.id) 
GROUP BY ds.id, com.approved, ds.approved 
ORDER BY ds.date_added ASC

That is removing the WHERE clause. But that is bad as it will select both designs and comments that haven’t been approved.

What do I miss / do wrong?

  • 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-06-05T04:48:34+00:00Added an answer on June 5, 2026 at 4:48 am

    Move all filters on comments to the ON clause:

    SELECT  ds.*, COUNT(com.design_id) AS countcom
    FROM    tdic_designs ds
    LEFT JOIN
            tdic_comments com
    ON      com.design_id = ds.id
            AND com.approved = 1 
    WHERE   ds.approved = 1
            AND ds.hidden = 0
    GROUP BY
            ds.id
    ORDER BY
            ds.date_added ASC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a little Problem. I have seven <select> 's. The go from left
I have a little problem in VBA, I want to put row and column
I have a little problem, have the following statement in mysql: SELECT cmfilm.*, cmgenre.titel
I have a problem that I can't figure out myself. I've tried using LEFT
I got a little problem. I'm using Full outer Join in Sql server -
basically I have a little problem with a background I am using. I need
Using the following query: SELECT pe.prodtree_element_name_l, MAX(rs.resource_value) AS resource_value FROM prodtree_element pe LEFT JOIN
i have a little problem. I'm trying to create in Delphi7 a list of
I have a little problem with the GtkFileChooserDialog. I Want to hide the favoris
I have a little problem with my if(isset($_POST['submit'])) code. What I want is some

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.