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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:12:16+00:00 2026-06-13T07:12:16+00:00

I have a MySQL query that I would like to enhance by requiring that

  • 0

I have a MySQL query that I would like to enhance by requiring that all values of client.client_name are printed out in the result, even if no values are found for every row in that table. The current table shows:

client.client_name
Client A
Client B
Client C

The current MySQL query is below:

    SELECT X.expr1 AS 'Project Name', SUM(X.expr2) AS 'Total Hours Logged', X.expr3 - sum(X.expr2) AS 'Monthly Hours Remaining', X.expr4 AS 'Last Day', DATEDIFF(X.expr4 , curdate()) AS 'Days Remaining'
FROM 
(SELECT 
    client.client_name AS expr1
    , sum(time_records.value) AS expr2
    , client.monthly_hours AS expr3
FROM project_objects
  INNER JOIN projects
    ON projects.id = project_objects.project_id
  INNER JOIN time_records
    ON time_records.parent_id = project_objects.id
  LEFT JOIN client  
  ON project_objects.project_id = client.project_id 
  WHERE time_records.parent_type = 'Task' 
  AND client.start_day_of_month < dayofmonth(curdate())
  AND time_records.state = 3
  GROUP BY client.client_name

  UNION

  SELECT 
    client.client_name AS expr1
    , sum(time_records.value) as expr2
    , client.monthly_hours AS expr3
  FROM projects
  INNER JOIN time_records
    ON projects.id = time_records.parent_id
  LEFT JOIN client  
   ON projects.id = client.project_id 
  WHERE time_records.parent_type = 'Project'
  AND client.start_day_of_month < dayofmonth(curdate())
  AND time_records.state = 3
  GROUP BY client.client_name
) X
GROUP BY X.expr1
ORDER BY DATEDIFF(X.expr4 , curdate()

As you can see from the above query – I added a Left Join for the client table, however it doesn’t result in printing out all client records – it only prints those for which there are time_records available. I think this is related to the nesting or the order of how I am writing the joins, but can’t seem to figure it out. If you have any ideas it would be greatly appreciated. Thanks!

  • 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-13T07:12:17+00:00Added an answer on June 13, 2026 at 7:12 am

    Please try order as below:

    FROM client LEFT JOIN project_objects 
        ON project_objects.project_id = client.project_id 
    

    see SQLFiddle for the final solution here: http://sqlfiddle.com/#!2/30362/16

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

Sidebar

Related Questions

I have a MySQL query that I would like to optimize a little bit.
I have a mysql db query that I'd like to take and condense into
I have a MySQL query that I'd like help optimizing. Its rather simple in
I have a MySQL query that results in something like this: person | some_info
I have created this table with a sql query that looks like this $result
I have this MySQL query that I'd like to optimize: SELECT min(`topic_id`) FROM `gallery_topics`
I've got the following MySQL query that I would like to translate so it
I have this MySQL query that is very slow, I presume because of all
I have a mysql query that targets a single column in a single row
I have a MySQL query that: gets data from three tables linked by unique

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.