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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:07:33+00:00 2026-05-27T14:07:33+00:00

I have two db tables: tasks: task_id (unique id, autoincrement) group_id (from the next

  • 0

I have two db tables:

tasks:
task_id (unique id, autoincrement)
group_id (from the next table: groups, where task belongs to)
task_name
status (if task has been completed or not)

groups:
group_id (unique id, autoincrement)
group_name

My goal is to list all groups along with number of uncompleted tasks in them (task status 0).
I achieve that by this query:

$sql_query = "SELECT g.id, g.name, count(g.id) as count
FROM task_groups g, tasks t
WHERE g.status = 0 AND t.group =  g.id AND t.status = 0
GROUP BY g.id
ORDER BY g.id ASC";

And I get results like this:

Group1  3 uncompleted
Group2 13 uncompleted
Group3 30 uncompleted

But empty task groups (there are no any tasks that belong to a specific group) are not being listed.

Result should be like this:

Group1  3 uncompleted
Group2 13 uncompleted
Group3 30 uncompleted
Group4  0 uncompleted
Group5  0 uncompleted

How can I achieve that?

  • 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-27T14:07:34+00:00Added an answer on May 27, 2026 at 2:07 pm

    You’re on the right track. GROUP BY the group, but instead of counting the group id, try instead doing a SUM on tasks that are incomplete, like this:

    SELECT g.id, g.name,
    SUM(IF(t.status = 0, 1, 0)) incomplete_tasks
    FROM task_groups g
    LEFT JOIN tasks t ON (t.group_id = g.id)
    GROUP BY g.id ASC
    HAVING incomplete_tasks > 0;
    

    Omit the HAVING clause if you want to list all task groups.

    Note: I didn’t see the purpose of g.status in your query, and status wasn’t listed as a column in the groups table so I’ve left it out of my example.

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

Sidebar

Related Questions

We have two tables - Tasks and TasksUsers (users assigned to task). Task has
I have a task_id table which has two columns: `tid`:task id, `state`:0:unfinished,1:processing,2:finished It's easy
I have two tables, tasks and projects. And every task has a project (and
I have two tables containing Tasks and Notes, and want to retrieve a list
I have two tables: Table 1: ID, PersonCode, Name, Table 2: ID, Table1ID, Location,
I have two tables, Tasks and TaskMilestones. I want a query to return the
I have two tables one to store a task and one to evaluate the
I have two tables. First one: create table history ( id integer primary key,
I have two tables : create table CurrentDay ( ID int identity primary key,
Ok so I have a table Tasks -- TaskId (unique autoinc primary) ChildOf (Contains

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.