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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:02:34+00:00 2026-06-10T06:02:34+00:00

I need to get data from multiple tables from a database and I need

  • 0

I need to get data from multiple tables from a database and I need to use 1 query. The trouble I am having is that I need to count how many tasks there are for each project and how many tasks are finished.

I got these table:
projects:

id    name        start_date           end_date             project_leader    finished
1     project_1   2012-08-01 00:00:00  2012-29-01 00:00:00  2                 0

users

id    username    password    email      status
1     user_1      pass_1      email_1    1
2     user_2      pass_2      email_2    1

user_has_project

userid   projectId
1        1

tasks

id   project  description          end_date               user  finished
1    1        test description 1   2012-29-01 00:00:00    1     1
2    1        test description 2   2012-29-01 00:00:00    1     0

So what I need to do, is make a query that should give me this result:
Wanted Result:

project_id  project_name    start_date           end_date             project_leader    finished       tasks      finished_tasks
1           project_1       2012-08-01 00:00:00  2012-29-01 00:00:00  user_2            0              2          1

I got it to work until the part where I need to count the amount of tasks that are finished. I got this query so far, but it doesn’t count the finished tasks yet. How can I do this?

Query:

SELECT projects.id,
       projects.name,
       projects.start_date,
       projects.end_date,
       projects.finished,
       users.username AS project_leader,
       COUNT(tasks.id) AS tasks
FROM projects
LEFT JOIN tasks ON (tasks.project = projects.id)
JOIN user_has_project ON (user_has_project.projectId = projects.id)
JOIN users ON (projects.project_leader = users.id)
WHERE user_has_project.userId = 1
GROUP BY projects.id
  • 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-10T06:02:36+00:00Added an answer on June 10, 2026 at 6:02 am

    I’m jumping the gun a little bit here (I’ll continue contemplating in case this gut-reaction is wrong), but the trick might be to use a “null if false” evaluation inside the count() function, which is documented to only count non-null values:

    SELECT projects.id,
           projects.name,
           projects.start_date,
           projects.end_date,
           projects.finished,
           users.username AS project_leader,
           COUNT(tasks.id) AS tasks,
           COUNT(NULLIF(tasks.finished, 0)) as finished_tasks
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to get data from multiple tables from a database and I need
I need to get data from several tables, so I used a query with
I have 3 tables, I need advice on how to get data from them.
I have huge amount of data from database, i need to get or store
I have a very basic need to get some data from the database and
Greetings, Here is my problem. I need to get data from multiple rows and
I need to search across multiple columns from two tables in my database using
Is there a best practice in getting data from multiple database tables using Zend?
I need to get data from xml file <?xml version=1.0?> <xml> <User> <Agent> <id>cr4523</id>
I need to get data from flat files to DB using SSIS . The

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.