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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:15:09+00:00 2026-06-03T22:15:09+00:00

I have a table of customers, orders, and items. I’d like to write a

  • 0

I have a table of customers, orders, and items. I’d like to write a report with one customer per row (for this example I’m assuming one order per customer) and whether certain items were ordered.

customers:

id  
firstName

orders:

id
customers_id

items:

id
orders_id

I’m having problems displaying a row if a particular item has not been ordered (e.g. apples)

What I’d like is a report with 3 columns:

  • First Name
  • Apples
  • Bananas

Here is the query I’m using:

SELECT c.firstName, i_apple, i_banana
FROM customers
LEFT JOIN orders o ON c.id = o.customers_id
LEFT JOIN items i_apple ON o.id = i_apple.orders_id
LEFT JOIN items i_banana ON o.id = i_banana.orders_id
WHERE i_apple = 'apple' AND i_banana = 'banana'
  • 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-03T22:15:11+00:00Added an answer on June 3, 2026 at 10:15 pm

    Everything is fine, but you need to filter outer joins in ON clause of the join, otherwise you turn outer into an inner join. I’ve added Name column to Items but it should probably be ProductID, a foreign key to Product table with ProductName.

    SELECT c.firstName, i_apple.Name Apple, i_banana.Name Banana
    FROM customers
    LEFT JOIN orders o 
      ON c.id = o.customers_id
    LEFT JOIN items i_apple 
      ON o.id = i_apple.orders_id
     AND i_apple.Name = 'apple'
    LEFT JOIN items i_banana 
      ON o.id = i_banana.orders_id
     AND i_banana.Name = 'banana'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a customers table, and an orders table. Each customer can have many
Lets say I have a Customers table and an Orders table with a one-to-many
If I have a Customers table linked to an Orders table, and I want
Lets say that I have three tables, customers, orders and orderDetails. I'm doing this:
If I have an items_ordered table that looks like this: items_ordered customerid order_date item
I have this GUI that shows, let's say Customer Orders. When my client nailed
Hopefully I can explain this correctly. I have a table of line orders (each
I have data that looks like this. SoldToRetailer OrderDate | Customer | Product |
I have 3 tables: Customers ID_CUSTOMER NAME Products ID_PRODUCT PRODUCTNAME PRICE Orders ID_ORDER CUSTOMER_ID
If I have two tables, Customers and Orders, and I want to look up

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.