mysql查询左连接+计数

时间:2012-01-12 01:56:12

标签: mysql

我想要一些来自订单文件的数据给我 客户编号,订单编号,产品,数量,价格以及每个订单的订单明细文件中的行数。 我在最后一部分遇到了麻烦。

Select Header.CustNo, Header.OrderNumber, Detail.SKU, 
   Detail.Quantity, Detail.Price, Count(Detail2) from Header
   left join Detail on (Header.OrderNumber= Detail.OrderNumber )
   left join Detail as Detail2 on (Header.OrderNumber= Detail2.OrderNumber )

1 个答案:

答案 0 :(得分:2)

这是什么意思? :

SELECT Header.CustNo,
       Header.OrderNumber,
       Detail.SKU, 
       Detail.Quantity,
       Detail.Price,
       ( SELECT COUNT(1)
           FROM Detail AS Detail2
          WHERE Detail2.OrderNumber = Header.OrderNumber
       )
  FROM Header
  LEFT
  JOIN Detail
    ON Header.OrderNumber = Detail.OrderNumber
;