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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:58:08+00:00 2026-06-02T16:58:08+00:00

I have a result set that I obtain from the join between three tables

  • 0

I have a result set that I obtain from the join between three tables using:

SELECT X.*, Y.Name FROM `A` AS X
INNER JOIN `B` AS Y
INNER JOIN `C` AS Z
ON X.id=Y.id AND X.categoryID=Z.categoryID
WHERE X.userID_FK=%d AND X.listID_FK=%d 
ORDER BY Z.categoryRank ASC

I have a fourth table ‘D’ which contains:

-------------------------------
  id_FK    userID_FK    vote
-------------------------------

Here (id_FK, userID_FK) pair is a PK.

Each X.id returned by the first query could have multiple entries in the D table. Normally, for each returned row, in order to count votes per id, I’d write a query like:

SELECT SUM(vote) FROM D WHERE `id`=%d

I am wondering if I could optimize these two queries into a single query as the current scheme results in O(n) execution time. n being the number of results returned by the first query.

  • 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-02T16:58:10+00:00Added an answer on June 2, 2026 at 4:58 pm

    You can simply do it like this

    SELECT
    X.*,
    Y.Name,
    (SELECT
       SUM(vote)
       FROM D
       WHERE `id` = X.id) as Count
    FROM `A` AS X
    INNER JOIN `B` AS Y
    INNER JOIN `C` AS Z
    ON X.id = Y.id
      AND X.categoryID = Z.categoryID
    WHERE X.userID_FK =  % d
    AND X.listID_FK =  % d
    ORDER BY Z.categoryRank ASC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a result set that is being returned using this code: while ($row
I have a result set that might look like this: ID (no column name)
I have a SOAP result set that the nuSoap extension has turned into a
I have a query that returns a result set similar to the one below:
I have a MYSQL stored procedure SP1() that returns a result set. I want
I have a result set that will never exceed 500; the results that come
I have a result set of data that I want to write to an
I have a stored procedure in Oracle that returns result set(s) via OUT parameter(s)
I have a result set that I want to trim a 2 digit suffix
I have a table that records distance from Google/Bing between two valid UK postcodes.

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.