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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:17:24+00:00 2026-06-01T12:17:24+00:00

I need to combine my main query with a subquery: Current query (which works):

  • 0

I need to combine my main query with a subquery:

Current query (which works):

SELECT *,(SELECT SUM(shift_length)FROM overtime_list
WHERE overtime_list.user_ID = users.user_ID AND overtime_list.date > '$totalovertimedays') AS overtime_total FROM availability_list
JOIN users ON users.user_ID = availability_list.user_ID
JOIN stations ON users.station_ID` = stations.station_ID
WHERE availability_list.date = '$date' AND type = '$type'
ORDER BY overtime_total ASC

($date and $type are vars filled in by PHP)

The query I want to insert this as a sub-query:

SELECT role_ID, GROUP_CONCAT(users_roles.role_ID) AS roles FROM users
JOIN users_roles ON users.user_ID = users_roles.user_ID
GROUP BY users.users_ID

I got this far – but I cant seem to get the GROUP BY to work, so it doesnt list them correctly:

SELECT *,(SELECT SUM(shift_length)FROM overtime_list
WHERE overtime_list.user_ID = users.user_ID AND overtime_list.date > '$totalovertimedays') AS overtime_total FROM availability_list
JOIN users ON users.user_ID = availability_list.user_ID
JOIN `stations` ON `users`.`station_ID` = `stations`.`station_ID`,
(SELECT role_ID, GROUP_CONCAT(users_roles.role_ID separator ', ') AS roles FROM users JOIN users_roles ON users.user_ID = users_roles.user_ID GROUP BY users.user_ID) AS roles
WHERE availability_list.date = '$date' AND type = '$type'
ORDER BY overtime_total ASC

availability_list table:

 +----------+---------+
 |  user_ID | user    |
 +----------+---------+
 |        1 |   Smith |
 +----------+---------+
 |        2 |   Jones |
 +----------+---------+
 |        3 |   Greg  |
 +----------+---------+

overtime_list table:

 +----------+----------------+------------+
 |  date    | shift_length   |   user_ID  |
 +----------+----------------+------------+
 |  1/1/11  |   5            |      1     |
 +----------+---------+------+------------+
 |  1/2/11  |   5            |      2     |
 +----------+---------+------+------------+
 |  1/6/11  |   2            |      2     |
 +----------+---------+------+------------+
 |  1/8/11  |   5            |      1     |
 +----------+---------+------+------------+
 |  1/12/11 |   2            |      1     |
 +----------+---------+------+------------+

users_roles table:

 +----------+---------+
 |  user_ID | roles   |
 +----------+---------+
 |        1 |   Admin |
 +----------+---------+
 |        2 |   Staff |
 +----------+---------+
 |        2 |   Admin |
 +----------+---------+
 |        2 |   Super |
 +----------+---------+
 |        1 |   Other |
 +----------+---------+

So the result would be:

 +----------+---------+----------------------------+------------------+
 |  user_ID | user    |   roles                    |  overtime_total  |
 +----------+---------+----------------------------+------------------+
 |        1 |   Smith |    Admin, Other            |        12        |
 +----------+---------+----------------------------+------------------+
 |        2 |   Jones |    Staff, Admin, Super     |        7         |
 +----------+---------+----------------------------+------------------+
 |        3 |   Greg  |                            |  null (or zero)  |
 +----------+---------+----------------------------+------------------+
  • 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-01T12:17:25+00:00Added an answer on June 1, 2026 at 12:17 pm

    This should work –

    SELECT users.*,
        SUM(overtime_list.shift_length) AS overtime_total,
        (SELECT GROUP_CONCAT(users_roles.role_ID) FROM users_roles WHERE users.user_ID = users_roles.user_ID) AS roles
    FROM availability_list
    INNER JOIN users
        ON users.user_ID = availability_list.user_ID
    INNER JOIN `stations`
        ON `users`.`station_ID` = `stations`.`station_ID`
    INNER JOIN overtime_list
        ON overtime_list.user_ID = users.user_ID
        AND overtime_list.date >= '$totalovertimedays'
    WHERE availability_list.date = '$date'
    AND type = '$type'
    GROUP BY users.user_ID
    ORDER BY overtime_total ASC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine a table for articles. In addition to the main query: SELECT * From
Say I have a character a which I need to combine with a symbol
Hello I currently 2 tables of data from different sources, I need to combine
I need to combine to a string column and the result of a row_number()
I need to combine lines in two files, basing in the condition, that in
I need to create and combine several expressions for child entity into one to
I need to convert the function path::combine(path1, path2). Please help me if you have
Need to take a SELECT drop down list options and find if any of
How would I combine the following two statements to create a valid SQL query?
I have two lists that i need to combine where the second list has

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.