0

我有两张桌子:

Discounts(disid primary key)

Cust(custid primary key, disid ref discount(disid))

现在我需要一个查询来获得custid所有disid(折扣券),并且客户可能disid不止一次包含相同的东西。

4

3 回答 3

1

尝试以下任一解决方案:

SELECT  a.custid, COUNT(a.disid) totalCoupon
FROM    cust a  
            INNER JOIN discounts b 
                ON b.disid = a.disid 
GROUP BY a.custid

或者

SELECT  a.custid, COUNT(a.disid) totalCoupon
FROM    cust a  
            INNER JOIN discounts b 
                ON b.disid = a.disid 
GROUP BY a.custid
HAVING   COUNT(a.disid) > 1 -- customers having the same (but more than 1) 
                            -- CouponID will only be shown here
于 2012-08-04T14:54:10.833 回答
1
select custid, count(distinct disid) from cust
group by custid
having count(*) = (select count(*) from discounts)
于 2012-08-04T14:47:27.143 回答
1
SELECT COUNT(DISTINCT D.disid) FROM CUST C 
INNER JOIN DISCOUNTS D ON D.disid=C.disid GROUP BY D.disid
于 2012-08-04T14:49:03.570 回答