我有3张桌子:
customers- with columns customer_id(Primary key),first_name, middle_name,last_name,email(唯一),密码,first_visit。
products- with columns product_id(Primary Key),product_name (独特),安全警告,公司。
购买 - 使用列purchase_id(主键),customer_id (外键),product_id(外键),日期。
如何生成一个特定公司的所有购买清单,例如" Company1",显示:客户的first_name,product_name和客户购买的该产品的safety_warning。我会使用内连接吗?
答案 0 :(得分:0)
您需要在purchases
和products
表格上执行两个内部联接。
SELECT c.first_name, pr.product_name, pr.safety_warning
FROM CUSTOMERS c
INNER JOIN PURCHASES pu
ON c.customer_id = pu.customer_id
INNER JOIN products pr
ON pr.product_id = pu.product_id
WHERE pr.company = "Company 1"
答案 1 :(得分:0)
一些事情:
SELECT customers.first_name, products.product_name, products.safety_warning
FROM customers JOIN purchases JOIN products
ON (customers.customer_id = purchases.customer_id AND
products.product_id = purchases.product_id)
WHERE company = "Company1"
应该有效
答案 2 :(得分:0)
这应该也可以。你现在有很多选择。
SELECT C.first_name, PR.product_name, PR.safety_warning
FROM purchases PU
join products PR on PU.product_id =PR.product_id
join customers C on PU.customer_id =C.customer_id
where PR.company="COMPANY1"