Reputation: 3
I have been attempting to combine these two SQL statements into one. However, I am having some trouble with it. Here they are:
string sql1 = "SELECT products.product_name
FROM products, order_details
WHERE products.id=order_details.product_id;";
string sql2 = "SELECT customers.first_name
FROM customers, orders
WHERE orders.customer_id=customers.id;";
I have been looking at using joins; however, I am getting confused on how to implement the WHERE statements.
Upvotes: 0
Views: 29
Reputation: 782702
Use ANSI JOIN, then it's easy to see how to do multiple joins by relating each table in the sequence to another table.
SELECT c.first_name, p.product_name
FROM customers AS c
JOIN orders AS o ON c.id = o.customer_id
JOIN order_details AS od ON o.id = od.order_id
JOIN products AS p ON p.id = od.product_id
Upvotes: 3