我有一张像吼叫一样的桌子:
我需要客户满足以下条件。
一个。谁下订单并支付全部费用。即,customer_id:34,42,70
湾谁下订单但没有付款。即,customer_id:57,80
℃。谁下订单但没有支付其中一些。即,customer_id:45,62,65
注意:invoice_no:0表示未支付订单。
需要专家的建议。 感谢
答案 0 :(得分: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
答案 1 :(得分: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