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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:03:22+00:00 2026-05-24T09:03:22+00:00

I’ve been struggling with a query which selects from multiple tables. My original query

  • 0

I’ve been struggling with a query which selects from multiple tables. My original query was incredibly slow (53 seconds). From reading up, I’m now reasonably sure that I need to create an inner query to limit the data which is iterated over. But I’m not sure how to use the result of the subquery (inner query) when using more than 2 tables. Below are some dummy tables:

+-------+---------------------+------------+
| tr_id | tr_datecreated      | tr_depart  |
+-------+---------------------+------------+
|     1 | 2011-07-31 00:00:00 | 2011-08-20 |
|     2 | 2011-08-01 00:00:00 | 2011-08-30 |
|     3 | 2011-08-02 00:00:00 | 2011-09-01 |
+-------+---------------------+------------+

+------+--------+---------+---------+
| p_id | p_trid | p_name  | p_lname |
+------+--------+---------+---------+
|    1 |      1 | Geoff   | Thingy  |
|    2 |      1 | Mildred | Thingy  |
|    3 |      1 | Garry   | Thingy  |
|    4 |      2 | Linda   | Doobrey |
|    5 |      2 | Kev     | Doobrey |
|    6 |      3 | John    | Wotsit  |
|    7 |      3 | Jill    | Wotsit  |
+------+--------+---------+---------+

+------+--------+----------+
| h_id | h_trid | h_dest   |
+------+--------+----------+
|    1 |      1 | France   |
|    2 |      1 | Spain    |
|    3 |      2 | Italy    |
|    4 |      3 | Portugal |
+------+--------+----------+

I want to get a result such as:

+-------+---------------------+------------+---------+---------+----------+
| tr_id | tr_datecreated      | tr_depart  | p_name  | p_lname | h_dest   |
+-------+---------------------+------------+---------+---------+----------+
|     1 | 2011-07-31 00:00:00 | 2011-08-20 | Geoff   | Thingy  | France   |
|     1 | 2011-07-31 00:00:00 | 2011-08-20 | Geoff   | Thingy  | Spain    |
|     1 | 2011-07-31 00:00:00 | 2011-08-20 | Mildred | Thingy  | France   |
|     1 | 2011-07-31 00:00:00 | 2011-08-20 | Mildred | Thingy  | Spain    |
|     1 | 2011-07-31 00:00:00 | 2011-08-20 | Garry   | Thingy  | France   |
|     1 | 2011-07-31 00:00:00 | 2011-08-20 | Garry   | Thingy  | Spain    |
|     2 | 2011-08-01 00:00:00 | 2011-08-30 | Linda   | Doobrey | Italy    |
|     2 | 2011-08-01 00:00:00 | 2011-08-30 | Kev     | Doobrey | Italy    |
|     3 | 2011-08-02 00:00:00 | 2011-09-01 | John    | Wotsit  | Portugal |
|     3 | 2011-08-02 00:00:00 | 2011-09-01 | Jill    | Wotsit  | Portugal |
+-------+---------------------+------------+---------+---------+----------+

where we get a separate row for each person for each holiday destination.

My original effort was in the form of:

SELECT tr_id, tr_datecreated, tr_depart, p_name, p_lname, h_dest
FROM transaction, people, holiday
WHERE tr_id = p_trid
AND tr_id = h_trid
AND tr_datecreated >= "2010-12-12 00:00:00"
AND tr_datecreated <= "2012-12-12 00:00:00"

I think that this created a huge number of cross joins and the query ran very slowly.

Seeing as the tr_id is being referenced a number of times I wanted to do an inner query which reduced the number of rows that everything else was compared to.

So the inner query part will be:

SELECT tr_id WHERE tr_datecreated >= "2010-12-12 00:00:00" 
AND tr_datecreated <= "2012-12-12 00:00:00"

How would I create my desired table which I would want to compare both the p_trid and the h_trid against the same inner query without running that inner query twice (if possible)?

Would inner joins help in this situation? (I have read through but haven’t fully absorbed it yet).

Grateful for any advice and suggestions here. The database is large and I need to be efficient.

Edit

Indexes:

tr_id, h_id and p_id are all primary keys

Result of EXPLAIN

+----+-------------+--------------+--------+---------------+---------+---------+---------------------+------+--------------------------------+
| id | select_type | table        | type   | possible_keys | key     | key_len | ref                 | rows | Extra                          |
+----+-------------+--------------+--------+---------------+---------+---------+---------------------+------+--------------------------------+
|  1 | SIMPLE      | holiday      | ALL    | NULL          | NULL    | NULL    | NULL                |    4 |                                |
|  1 | SIMPLE      | people       | ALL    | NULL          | NULL    | NULL    | NULL                |    7 | Using where; Using join buffer |
|  1 | SIMPLE      | transactions | eq_ref | PRIMARY       | PRIMARY | 4       | db.people.p_trid |    1 | Using where                    |
+----+-------------+--------------+--------+---------------+---------+---------+---------------------+------+--------------------------------+
  • 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-24T09:03:23+00:00Added an answer on May 24, 2026 at 9:03 am

    I think that this should work. Let me know if it works.

    Total Query

    SELECT t.id, t.date, t.depart, p.p_name, p.p_lname, h.h_dest 
    FROM
    (SELECT tr_id 'id', tr_datecreated 'date', tr_depart 'depart' FROM transaction 
    WHERE DATE(tr_datecreated) BETWEEN DATE("2010-12-12 00:00:00") 
    AND DATE("2012-12-12 00:00:00")) t
    JOIN people p  ON t.id = p.p_trid 
    JOIN holiday h ON t.id = h.h_trid;
    

    Inner Query

    (SELECT tr_id 'id', tr_datecreated 'date', tr_depart 'depart' FROM transaction 
    WHERE DATE(tr_datecreated) BETWEEN DATE("2010-12-12 00:00:00") 
    AND DATE("2012-12-12 00:00:00"))
    

    Edit: Subquery explanation

    The subquery selects the id, date created, and depart columns from the transaction table for the date range that you listed above. The ‘t’ outside the right paren at the end of query lets you alias the inner query so you can use its data above. Also, where I have 'id','date', and 'depart' inside the subquery is also aliasing. It lets you use those values without typing out the full column name.

    Hope this helped.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string

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.