MYSQL LEFT JOIN加入到最后一行的第二个表

时间:2017-08-30 06:53:35

标签: mysql sql database

我们有三张桌子

1. customer 
2. custumerorder 
3. order_type

客户: custumer_id,custumer_name,custumer_address,custumer_phone,status

ORDER_TYPE order_type_id,订单类型,

custumerorder custumerorder_id,custumer_id,order_type_id,order_date,Total_order,shipping_info

我想查看最后的订单详情 所以我有查询

SELECT c.*,
       tblcusorder.order_dt,
       tblcusorder.orderType,
       tblcusorder.shipping_info
FROM customer c
LEFT JOIN
  ( SELECT max(cr.order_date) AS order_dt,
           ot.orderType,
           cr.custumer_id,
           cr.custumerorder_id,
           cr.order_type_id,
           cr.shipping_info
   FROM custumerorder cr
   INNER JOIN order_type ot ON ot.order_type_id=cr.order_type_id
   GROUP BY cr.custumer_id ) AS tblcusorder ON tblcusorder.custumer_id=c.custumer_id
WHERE c.status='TRUE'

问题

因为我在订单日期使用Max()函数,所以它显示最后的订单日期是好的,但其余的行数据不是来自该行(客户订单),而且ordertype与该日期没有关联。

0 个答案:

没有答案