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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:18:42+00:00 2026-05-31T12:18:42+00:00

I am trying to get data from multiple tables at once. In my old

  • 0

I am trying to get data from multiple tables at once. In my old system I would make a query, get the data I needed, and then use it for the next query. I wanted to avoid making multiple queries so I combined them with joins:

 SELECT `to`.`id` , `to`.`group`, `to`.`email`, `pTo`.`min_offer`, COUNT(`offerRes`.`id`), `settingsRes`.`email`
                                FROM `members` AS `to`

                                LEFT JOIN (
                                  SELECT `id` , `min_offer`
                                  FROM `profile`
                                ) pTo ON pTo.id = to.id

                                LEFT JOIN (
                                  SELECT `id`, `from`,`to`
                                  FROM `offers`
                                ) offerRes ON offerRes.from = ? && offerRes.to = to.id

                                LEFT JOIN (
                                  SELECT `id`, `email`
                                  FROM `user_settings`
                                )  settingsRes ON settingsRes.id = to.id

                                WHERE to.id = ? LIMIT 1

I use the ID that is passed in (to.id) to obtain results from the profiles table and the offers table and the user settings table. These tables all have a PRIMARY id to which everything is mapped to. My PHP users prepared statements however I replaced the ? with hard values in PHPMyAdmin. My EXPLAIN is as follows:

id  select_type     table   type    possible_keys   key     key_len     ref     rows    Extra
1   PRIMARY         to     const    PRIMARY       PRIMARY   4         const     1   
1   PRIMARY     <derived2>  ALL     NULL            NULL    NULL    NULL        45  
1   PRIMARY     <derived3>  ALL     NULL            NULL    NULL    NULL        1   
1   PRIMARY     <derived4>  ALL     NULL            NULL    NULL    NULL        15  
4   DERIVED     user_settingsALL    NULL            NULL    NULL    NULL        15  
3   DERIVED     offers      system  NULL            NULL    NULL    NULL        1   
2   DERIVED     profile     ALL     NULL            NULL    NULL    NULL        45  

Why is MySQL going through all 45 rows in the profile field when it can do a simple lookup as my query is based on the PRIMARY key id? Any way to make this more efficent? All I want is a table of the following:

id (from members) - this is optional as this is what is obtained from the user and passed in to SELECT against
group (from members) - corresponding to the id that is passed in
email (from members) - corresponding to the id that is passed in
min_offer (from profile) - also corresponding to the id that is passed in
NUM_ROWS(from offers) - number of rows corresponding to the id taht is passed in and another number that is also passed in.  The offers table has a PRIMARY id but it is a different ID than what is passed in.
email (from user_settings) - corresponding to the id that is passed in
  • 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-31T12:18:44+00:00Added an answer on May 31, 2026 at 12:18 pm

    Why is MySQL going through all 45 rows in the profile field when it can do a simple lookup as my query is based on the PRIMARY key id?

    Because you’re joining to the results of sub-queries, which will include every record from the table:

    LEFT JOIN (
      SELECT `id` , `min_offer`
      FROM `profile`
    ) pTo ON pTo.id = to.id
    
    -- etc.
    

    Instead, join to the tables directly:

    LEFT JOIN `profile` AS pTo ON pTo.id = to.id
    
    LEFT JOIN `offers` AS offerRes ON offerRes.from = ? && offerRes.to = to.id
    
    LEFT JOIN `user_settings` AS settingsRes ON settingsRes.id = to.id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use jQuery to get data from an ASP.NET web service (SharePoint
I'm simply trying to get data from two sql server db tables using ado.net
I'm trying to extract data from multiple Tables in a Word document. When trying
I'm trying to get a MySQL query to give me related data 2 tables
What is the best way of handling trying to get data from a DataReader
I'm trying to get character data from www.wowarmory.com using PHP and cURL. The code
for example I'm trying to get the data from database like: using (ExplorerDataContext context
I have sugar crm instance and i was trying to get some data from
I'm trying to get the video data from this youtube playlist feed and add
I am trying to get some form data from POST method. Here's the code

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.