显示的MYSQL查询:每个客户的第一个订单

时间:2015-03-29 18:52:29

标签: php mysql

请帮帮我... 基于Northwind数据库http://www.pilpoils.be/demos/northwind-db/assets/12f53c79/northwind.gif, 我想编写一个MYSQL查询,显示:每个客户的第一个订单。 我试过但我有困难......这是我的疑问:

select top 1 * from orders inner join customers on orders.customerid=customers.customerid

thnx to all

1 个答案:

答案 0 :(得分:0)

SELECT * from Customers AS c 
    INNER JOIN `Orders` AS o ON c.CustomerID = o.CustomerID
    INNER JOIN 
        ( SELECT CustomerID, min(OrderDate) AS mod FROM `Orders` GROUP BY CustomerID ) AS mTable
        ON mTable.mod = OrderDate AND mTable.CustomerID = C.CustomerID;