列出客户名称和已下达超过4个订单的客户下达的订单数量

时间:2016-02-01 07:17:12

标签: mysql sql

有两个表:

  1. customercust_iddetails of customer address
  2. order - ord_idcust_idord_quantity
  3. 对于一个cust_id,有许多orders,即许多ord_id

1 个答案:

答案 0 :(得分:-1)

SELECT
    c.*,
    o.total_order
FROM    customer c
LEFT JOIN (
    SELECT
        cust_id,
        count(*) AS total_order
    FROM ORDER
    GROUP BY cust_id
    HAVING count(*) > 4
) o 
ON o.cust_id = c.cust_id
WHERE
    o.cust_id IS NOT NULL;

它应该返回欲望结果