Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Left Join Query

Left Join Query

Left Join Query


I have the following 2 tables;

Table1: Products (ID, Customer, ProductName, Price)
Table2: Orders (ID, Customers, ProductName, Price, Quantity)

I want to create a query that selects all the records from the Products table and also any matching records from the Orders table for a given customer. However, if there is a match in the Orders table, then I want the value in the Orders table to supercede the value from the Products table.


If CustomerA has the following 3 products in the Products table;

ProdA, £1.99
ProdB, £3.00
ProdC, £5.00

The orders table includes the following records for CustomerA

ProdA, £1.99, 15
ProdB, £3.99, 10

I now want to display a recordset from the above tables that looks like the following;

ProdA, £1.99, 15
ProdB, £3.99, 10
ProdC, £5.00, 15

Please note there was no record for ProdC in the Orders table, but this was still displayed and because there was a matching record for ProdB in the Orders table the price value was taken from the Orders table (£3.00 - £3.99).

I know I need a left join but can't get this to work!

Any help would be much appreciated!

Many Thanks,


RE: Left Join Query

I'm not clear on how you got 15 quantity for ProdC, since there wasn't a row in the orders table.  Other than that, this should work


SELECT p.productName, COALESCE( o.price, p.price ) price, o.quantity
FROM  `products` p
LEFT JOIN  `orders` o ON p.customer = o.customer
AND p.productName = o.productName

I cannot be bought.  Find leasing information at http://www.joshaxtell.com/

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close