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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:56:49+00:00 2026-06-14T19:56:49+00:00

I’m having trouble getting a query to work where data is pulled from two

  • 0

I’m having trouble getting a query to work where data is pulled from two tables, with a 3rd table in the middle. Let me show you what I mean.

*Table companies*
id int PK
name text 

*Table projects*
id int PK
company_id int FK
project_name text

*Table hours_worked*
id int pk
user_id int FK
project_id int FK
hours float
date_worked datetime

*Table users*
id int PK
user_name text

Basically, what I need is a query that pulls the total amount of hours worked per company based on a user ID.

Note that the table_hours worked can have multiple submissions per day per project. For example, a few rows might look like

id  project_id user_id  hours  date_worked
 1       1        1       2     20-08-2012
 2       1        1       1.5   20-08-2012
 3       2        1       3     21-08-2012
 4       2        2       12    22-08-2012

My desired result would be a query that returns something like this:

company_name     total_hours
 Bobs Kitchens       25
 Mikes Bikes         67

Which returns the total number of hours worked per company (not project) for say, a user with the user ID of 1.

Here is the following query I’ve tried with no avail:

        SELECT DISTINCT companies.name as company_name,
        companies.id as company_id,
        (
          SELECT SUM(hours_worked.hours) FROM hours_worked
          WHERE projects.id = hours_worked.project_id
          AND projects.company_id = company_id
        ) as total_hours
        FROM hoursworked, companies, projects
        WHERE projects.company_id = company_id
        AND projects.company_id = projects.company_id
        AND hours_worked.user_id = 1
        GROUP BY companies.id

This is giving me an odd result where a really weird number appears to be displaying for every total_hours field. 75 is not the correct total hours for any company!

company_name   total_hours
Mikes Kitchen      75
Charlies Bikes     75
 .....             75

Any help would be greatly appreciated!

  • 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-14T19:56:50+00:00Added an answer on June 14, 2026 at 7:56 pm

    I was such a fool..it was actually a lot easier than I expected. Hurray for over thinking.

    The query I’ve used is:

    SELECT SUM(hours_worked.hours) as 'total_hours',
                    companies.name
                    FROM companies, hours_worked, projects
                    WHERE companies.id = projects.company_id
                          AND hours_worked.project_id = projects.id
                          AND hours_worked.uid = 1
                          GROUP BY companies.id ORDER BY companies.name DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I've tracked down a weird MySQL problem to the two different ways I was
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.