简体   繁体   中英

Attempting To Combine These Two SQL Statements Into One

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM