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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:42:38+00:00 2026-06-08T20:42:38+00:00

How do I write an sql query to display items from 2 tables based

  • 0

How do I write an sql query to display items from 2 tables based on a selection of a criteria which is from a different table.

Tables I have:

  1. Customer table has columns CustomerID, Name, Address, Tel
  2. CustomerOrder table has columns CustomerID, OrderID, Date, TotalAmount, Status
  3. OrderItem table has columns OrderID, ProductCode, UnitPrice, Qty, TotalPrice

So when a CustomerID is selected, I want the orders to be displayed joining these 3 tables. so as below it should display all the orders that the customer has ever placed.
I tried using the query:

Select CustomerOrder.*, OrderItem.*
From CustomerOrder
INNER JOIN OrderItem Where Customer.CustomerID = $CustomerID

But it’s not working. Need help in the query and also in displaying the data properly using php.

Can anyone help?

E.g.

CustomerID:__________

OrderID:__1____ Date:______ TotalAmount:______ Status:_____
ProductCode:__ UnitPrice:___ Qty:_____TotalPrice:__________
ProductCode:___ UnitPrice:______ Qty:_____ TotalPrice:_________

OrderID:___2___ Date:______ TotalAmount:______ Status:_____
ProductCode:__ UnitPrice:___ Qty:_____TotalPrice:__________
ProductCode:___ UnitPrice:______ Qty:_____ TotalPrice:_________
  • 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-08T20:42:41+00:00Added an answer on June 8, 2026 at 8:42 pm

    Since you don’t need to display Customer information at this step, then you query can be :

    SELECT co.OrderID
    ,co.Date
    ,co.TotalAmount
    ,co.Status
    ,ci.ProductCode
    ,ci.UnitPrice
    ,ci.Qty
    ,ci.TotalPrice
    FROM CustomerOrder AS co
    INNER JOIN OrderItem AS ci ON (ci.orderID = co.OrderID)
    WHERE co.CustomerID = $customer
    ORDER BY co.OrderID, ci.ProductCode
    

    As you’d like to no repeat order information in your output, your PHP code should be soemthing like this:

    $current_order_id = false;
    foreach ($data as $row) {
       if ($current_order_id!==$row['OrderID']) {
            $current_order_id = $row['OrderID'];
            echo "OrderID: ".$row['OrderID']." , Date: ".$row['Date']." , TotalAmount: ".$row['TotalAmount']." , Status: ".$row['Status']." <br>";
       }
       echo "ProductCode: ".$row['ProductCode']." , UnitPrice: ".$row['UnitPrice']." , Qty: ".$row['Qty']." , TotalPrice: ".$row['TotalPrice']." <br>";
    }
    

    Anothere way of doing the same is to first get all the Orders information from CustomerOrder only.
    And then loop on the result and get items infotmation OrderItem for each order.

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

Sidebar

Related Questions

I need to write a SQL query which will get me those rows from
I have been asked in an interview to write a SQL query which fetches
I have a SQL Query which I am trying to write and am now
ive got a sql query which creates a table for books which stores data
I am retrieving data from a SQL table so I can display the result
I have a sql table which have the following data, Id City Country ---
I am trying to write a sql query which fetches all the tags related
I have a very simple SQL query that I made which retrieves some user
How would you write a SQL query to get the size of blob in
I want to write an SQL query using the LIKE keyword. It should search

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.