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

The Archive Base Latest Questions

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

I’m working on what is for me a complicated query, and I’ve managed to

  • 0

I’m working on what is for me a complicated query, and I’ve managed to get the information I need, but seem to be forced to create a table to accomplish it. I’m using MySQL, so I can’t use WITH, I can’t use a view because my SELECT contains a subquery in the FROM clause, and I can’t use a temporary table because I need to self-join. Am I missing something?

Background:

  • a reservation can have 1 or more reservation_detail (foreign key rel’p on reservation_id)
  • a reservation_detail has a quantity and a ticket_type (foreign key rel’p on ticket_type)

Here’s the first part of my current solution:

CREATE TABLE 
    tmp
SELECT 
    t.reservation_id, 
    t.ticket_type, 
    COALESCE(rd.quantity,0) AS qty
FROM (
    SELECT *
    FROM 
        (ticket_type tt, reservation r)
    ) t
LEFT JOIN 
    reservation_detail rd 
    ON 
        t.reservation_id = rd.reservation_id 
    AND 
        t.ticket_type = rd.ticket_type;

This gives me a table that looks like the following, where for each combination of a reservation_id and a ticket_type, I have a qty.

+----------------+-------------+------+
| reservation_id | ticket_type | qty  |
+----------------+-------------+------+
|              1 | ADULT       |    2 | 
|              1 | CHILD       |    2 | 
|              1 | INFANT      |    0 | 
|              2 | ADULT       |    1 | 
|              2 | CHILD       |    0 | 
|              2 | INFANT      |    0 | 
|              3 | ADULT       |    1 | 
|              3 | CHILD       |    0 | 
|              3 | INFANT      |    0 | 
+----------------+-------------+------+

Now I can self join thrice on this table to get what I’m really looking for…

SELECT 
    t1.reservation_id, 
    t1.qty AS num_adults, 
    t2.qty AS num_children, 
    t3.qty AS num_infants
FROM 
    tmp t1
LEFT JOIN 
    tmp t2 
    ON 
        t1.reservation_id = t2.reservation_id
LEFT JOIN 
    tmp t3 
    ON 
        t2.reservation_id = t3.reservation_id
WHERE 
    t1.ticket_type = 'ADULT'
AND 
    t2.ticket_type = 'CHILD'
AND 
    t3.ticket_type = 'INFANT';

…which is one row for each reservation showing the qty for each of the three ticket types.

+----------------+------------+--------------+-------------+
| reservation_id | num_adults | num_children | num_infants |
+----------------+------------+--------------+-------------+
|              1 |          2 |            2 |           0 | 
|              2 |          1 |            0 |           0 | 
|              3 |          1 |            0 |           0 | 
+----------------+------------+--------------+-------------+

I hope this is enough information. Please leave a comment if it’s not.

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

    If your query is considering only these 3 types: ADULT, CHILD, INFANT; you don’t have to use table ticket_type.

    SELECT 
        r.reservation_id, 
        COALESCE(rd_adult.quantity,0) AS num_adults, 
        COALESCE(rd_child.quantity,0) AS num_children, 
        COALESCE(rd_infant.quantity,0) AS num_infants
    FROM 
        reservation r
    LEFT JOIN 
        reservation_detail rd_adult 
        ON r.reservation_id = rd_adult.reservation_id
           and rd_adult.ticket_type = 'ADULT'
    LEFT JOIN 
        reservation_detail rd_child 
        ON r.reservation_id = rd_child.reservation_id
           and rd_child.ticket_type = 'CHILD'
    LEFT JOIN 
        reservation_detail rd_infant
        ON r.reservation_id = rd_infant.reservation_id
           and rd_infant.ticket_type = 'INFANT'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to clean up various Word 'smart' characters in user input, including but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I'm making a simple page using Google Maps API 3. My first. One marker
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.