2014-10-17 196 views
0

我需要显示cust_id,客户的姓和名,产品名称< - (来自产品表)和销售日期< - (来自销售表),还需要按顺序显示最近的日期第一次。inner-join mysql x3

这是我这么远:

SELECT 
    customer.cust_id, 
    customer.forename, 
    customer.surname, 
    products.prod_name, 
    sales.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 

任何帮助,将不胜感激。

+1

你应该是一致的,当你使用别名,在选择部分也改用他们的表名。除此之外,查询看起来是正确的(尽管不知道架构)。 – jpw 2014-10-17 15:12:28

+0

你正在创建表名的别名,所以你可能需要在select语句中相同,即'c.customer_id'..etc – 2014-10-17 15:12:53

回答

0

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 

将工作