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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:13:56+00:00 2026-05-24T23:13:56+00:00

I have the following tables setup in a MySQL database: Client: id name Session:

  • 0

I have the following tables setup in a MySQL database:

Client:
id
name

Session:
id
service_id
date

Attendance:
id
client_id
session_id

Service:
id
duration

Clients have a many to many relationship with Sessions through the Attendance table.

I am trying to write a query which will return each clients’ name and the total amount of time they have spent in sessions, like this:

Name       Total Time (Mins)
Person A   200
Person B   500
Person C   100

So far my query looks like this:

SELECT 
(SELECT SUM(services.duration) 
FROM services
INNER JOIN sessions
ON sessions.service_id = services.id
INNER JOIN attendances
ON attendances.session_id = session.id
INNER JOIN clients
ON clients.id = attendances.client_id
GROUP BY clients.id) AS total_duration,
client.name 
FROM clients

However, this returns no results. Whereas, if I run either part of the query in isolation I get the data I need, ie:

SELECT name FROM clients

OR

SELECT SUM(services.duration) 
FROM services
INNER JOIN sessions
ON sessions.service_id = services.id
INNER JOIN attendances
ON attendances.session_id = session.id
INNER JOIN clients
ON clients.id = attendances.client_id
GROUP BY clients.id

Can anyone see a reason why it would not be working when I combine the two?

  • 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-24T23:13:58+00:00Added an answer on May 24, 2026 at 11:13 pm

    You are missing a WHERE condition in correlated query. I rewrote your query with only joins to make it more readable:

    SELECT c.name, sum(srv.duration)
    FROM clients c
        JOIN attendances a ON a.client_id = c.id
        JOIN sessions s ON s.id - a.session_id
        JOIN services srv ON srv.id = s.service_id
    GROUP BY c.name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following tables in MySQL: team: id, name, [more stuff] person: id,
I have the following tables, with these keys in my database: bookings session_id sessions
I have the following tables in my database that have a many-to-many relationship, which
I have the following tables in SQL Server 2005 ReceiptPoint: ID (PK), Name GasIndexLocation:
I have the following tables in a database (i'll only list the important attributes):
I have the following setup. Spring 3.0.5 Hibernate 3.5.6 MySql 5.1 To save a
I have a MySQL table with following setup: CREATE TABLE IF NOT EXISTS `coords`
I have the following database tables: party, with a pk pty_id connected to a
I have the following tables: 1-Categories: -CategoryID -CategoryName -ParentID 2-Items: -ItemId -ItemName -CategoryID categories
I have the following tables. I wanted it so when I make more boxes

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.