我有一张如下表:
我需要客户满足以下条件。
一个。谁下订单并付清所有款项。即,customer_id:34、42、70
湾。谁下了订单但没有付款。即,customer_id: 57, 80
C。谁下了订单但没有支付其中一些。即customer_id:45、62、65
注意: invoice_no: 0 表示订单未付款。
需要专家的建议。谢谢
案例A
SELECT customer_id
FROM customers
GROUP BY customer_id
HAVING MIN(invoice_no) > 0
案例B
SELECT customer_id
FROM customers
GROUP BY customer_id
HAVING MAX(invoice_no) = 0
案例C
SELECT customer_id
FROM customers
GROUP BY customer_id
HAVING MIN(invoice_no) = 0 AND MAX(invoice_no) > 0
你可以试试这个
案例A
SELECT customers.* FROM customers INNER JOIN ( SELECT customer_id , MIN(invoice_no) minInvoice ,MAX(invoice_no) maxinvoice FROM customers GROUP BY customer_id ) c
ON c.customer_id = customers.customer_id AND c.minInvoice > 0
案例B
SELECT customers .* FROM customers INNER JOIN ( SELECT customer_id , MIN(invoice_no) minInvoice ,MAX(invoice_no) maxinvoice FROM customers GROUP BY customer_id) c
ON c.customer_id = customers.customer_id AND c.minInvoice = 0 AND c.maxinvoice = 0;
案例C
SELECT customers.* FROM customers INNER JOIN ( SELECT customer_id , MIN(invoice_no) minInvoice ,MAX(invoice_no) maxinvoice FROM customers GROUP BY customer_id ) c
ON c.customer_id = customers .customer_id AND c.maxinvoice > 0 AND c.mininvoice=0