1

我有 3 张桌子:

顾客

ID_CUSTOMER
NAME

产品

ID_PRODUCT
PRODUCTNAME
PRICE

订单

ID_ORDER
CUSTOMER_ID
PRODUCT_ID
QUANTITY

如何选择所有订购 $10k 或以上的客户?

4

4 回答 4

1
select c.*
from customers c
inner join orders o on o.customer_id = c.id_customer
inner join products p on p.id_product = o.product_id
group by c.id_customer
having sum(p.price * o.quantity) >= 10000
于 2012-06-19T07:17:53.213 回答
1

尝试类似:

SELECT C.ID_CUSTOMER, C.Name, 
      SUM(P.PRICE * O.QUANTITY) AS Total FROM Customers C 
      JOIN Orders O ON C.CUSTOMER_ID = O.CUSTOMER_ID
      JOIN Procucts P ON P.PRODUCT_ID = O.PRODUCT_ID
      GROUP BY C.ID_CUSTOMER, C.Name
      HAVING SUM(P.PRICE * O.QUANTITY) >= 10000
于 2012-06-19T07:20:56.360 回答
1

试试这个:

 SELECT   a.ID_CUSTOMER, 
          a.NAME
   FROM   CUSTOMERS a
            INNER JOIN ORDERS b 
              ON b.CUSTOMER_ID = a.ID_CUSTOMER
            INNER JOIN PRODUCTS c
              ON c.ID_PRODUCT = b.product_id
GROUP BY  a.ID_CUSTOMER, a.NAME
  HAVING  SUM(c.PRICE * b.QUANTITY) >= 10000
于 2012-06-19T07:30:46.213 回答
1

这并不是一个复杂的查询。做同样事情的另一种语法:

SELECT C.ID_CUSTOMER, C.NAME, SUM(P.PRICE * O.QUANTITY) TOTAL
  FROM Products P, Customers C, Orders O
 WHERE O.CUSTOMER_ID = C.ID_CUSTOMER 
   AND O.PRODUCT_ID = P.ID_PRODUCT
 GROUP BY C.ID_CUSTOMER, C.NAME
HAVING SUM(P.PRICE * O.QUANTITY) >= 10000
于 2012-06-19T07:41:06.737 回答