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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:49:06+00:00 2026-06-15T03:49:06+00:00

I have three tables here, that I’m trying to do a tricky combined query

  • 0

I have three tables here, that I’m trying to do a tricky combined query on.

Table 1(teams) has Teams in it:

id     name
------------
150    LA Lakers
151    Boston Celtics
152    NY Knicks

Table 2(scores) has scores in it:

id  teamid   week    score
---------------------------
1     150     5        75
2     151     5        95
3     152     5        112

Table 3(tickets) has tickets in it

id    teamids    week
---------------------
1   150,152,154   5   
2   151,154,155   5    

I have two queries that I’m trying to write
Rather than trying to sum these each time i query the tickets, I’ve added a weekly_score field to the ticket. The idea being, any time a new score is entered for the team, I could take that teams id, get all tickets that have that team / week combo, and update them all based on the sum of their team scores.

I’ve tried the following to get the results i’m looking for (before I try and update them):

SELECT t.id, t.teamids, (
  SELECT SUM( s1.score ) 
  FROM scores s1
  WHERE s1.teamid
   IN (
    t.teamids
   )
 AND s1.week =11
) AS score
FROM tickets t
WHERE t.week =11
AND (t.teamids LIKE  "150,%" OR t.teamids LIKE  "%,150")

Not only is the query slow, but it also seems to not return the sum of the scores, it just returns the first score in the list.

Any help is 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-15T03:49:07+00:00Added an answer on June 15, 2026 at 3:49 am

    If you are going to match, you’ll need to accommodate for the column only having one team id. Also, you’ll need to LIKE in your SELECT sub query.

    SELECT t.id, t.teamids, (
      SELECT SUM( s1.score ) 
      FROM scores s1
      WHERE 
        (s1.teamid LIKE t.teamids 
            OR CONCAT("%,",s1.teamid, "%") LIKE t.teamids 
            OR CONCAT("%",s1.teamid, ",%") LIKE t.teamids
        )
        AND s1.week =11
    ) AS score
    FROM tickets t 
    WHERE t.week =11
    AND (t.teamids LIKE  "150,%" OR t.teamids LIKE  "%,150" OR t.teamids LIKE "150")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So here's my problem. I have three tables: Person Table person_id, name Events Table
I am stuck with a mysql query. I have three tables: Table Name :
I have three tables in an SQL 2005 database, that I need to query
Ok here is the question I have three tables events - id - name
Here's my problem: I have three tables that represent a many to many relationship
This has been driving me mad. I have three tables: items ID name type
So, I have three tables, table local, table item and table item_local. Here is
I have three tables: Employee(EmployeeID,Fname,Lname...) ProjectHeader(ProjectID,LeadID,Status....) ProjectDetails(ProjectDetailsID,ProjectID....) Here's my current code: $get_projects = SELECT
Ok, here's the condensed form. I have three main tables to draw data from:
Rails noob here. I have a rails application with (in this example) three tables.

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.