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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:50:24+00:00 2026-05-20T17:50:24+00:00

I have three existing SQL tables we will call teams, miles, and riders. Leaving

  • 0

I have three existing SQL tables we will call “teams”, “miles”, and “riders”. Leaving out the fluff, their structure looks like this:

Table: teams
------------+-------------+---------+
| team_name | captains_id | team_id |
------------+-------------+---------+
| superbads | 11          | 1       |
| superflys | 12          | 2       |
------------+-------------+---------+

Table: riders
--------------+-----------+----------+
| rider_name  | team_id   | rider_id |
--------------+-----------+----------+
| donatello   | 1         | 10       |
| leonardo    | 1         | 11       |
| michelangelo| 2         | 12       |
| raphael     | 2         | 13       |
--------------+-----------+----------+

Table: miles
--------------+-----------+----------+
| rider_id    | miles     | id       |
--------------+-----------+----------+
| 10          | 100       | 1        |
| 10          | 62        | 2        |
| 11          | 110       | 3        |
| 11          | 100       | 4        |
| 12          | 8         | 5        |
| 12          | 22        | 6        |
| 13          | 29        | 7        |
| 13          | 2         | 8        |
--------------+-----------+----------+

I need to return a list of teams with total miles generated by that team (I also need to return the team captain’s name, but that’s a bit easier).
The difficulty is that I need to join miles on riders, sum the “miles” field, and then join that on teams somehow.

Changing the table structure is pretty much out, as this is an existing application. This is a LAMP environment, so manipulating PHP arrays after the query is an option if needed.

  • 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-20T17:50:25+00:00Added an answer on May 20, 2026 at 5:50 pm

    This should do it:

    select t.team_id, t.team_name, t.captains_id, sum(m.miles) as total_miles
    from teams t
    inner join riders r on r.team_id = t.team_id
    inner join miles m on m.rider_id = r.rider_id
    group by t.team_id, t.team_name, t.captains_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an existing database with tables which each have 4 partitions (there are
I have an existing ASP.NET MVC application with some sample data in the SQL
I have SQL query which joins 3 tables, one being just a many-to-many connecting
I have a database table with many columns. Is there sql that will update
In Guava, if I have a Collection<T> - is there any existing function in
We have an existing WordPress install with a thriving user base. There are also
If I have an existing solution containing multiple c# projects, are there any static
Have three classes User, Group and Field. Many to many relationship on User /
Have three divs in a container that I want to float over a large
i have three lists with the same number of elements in each other, i

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.