[英]inner-join mysql joining 3 tables
我在使用3個表的內部聯接時遇到問題..
我需要顯示cust_id,客戶的姓氏和姓氏,產品名稱<-(來自產品表)和銷售日期<-(來自銷售表),我還需要首先顯示最新日期。
這是我到目前為止所得到的
enter SELECT
customers.cust_id,
customers.forename,
customers.surname,
products.prod_name,
sales.Date_of_sale
FROM
customers
INNER JOIN
sales
ON
customers.cust_id = sales.cust_id; here
id真的很感激,如果您能在這里幫助我,謝謝..
只需在products
表中再添加一個JOIN
並包含ORDER BY
子句即可:
SELECT
c.cust_id,
c.forename,
c.surname,
p.prod_name,
s.Date_of_sale
FROM customers c
INNER JOIN sales s ON c.cust_id = s.cust_id
INNER JOIN products p ON s.product_id = p.product_id
ORDER BY s.Date_of_sale DESC
我認為問題出在您的FROM參數中。 您僅指定了客戶。
SELECT customers.cust_id, customers.forename, customers.surname, products.prod_name, sales.Date_of_sale
FROM
customers , products , sales
INNER JOIN
sales
ON
customers.cust_id = sales.cust_id;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.