0

我有表合同 c (id, exp_date) 和表成员 m (id, cid)。我需要计算所有合同和所有成员一起加入 m.cid = c.id 上的 2 个表。

我试过了,但显然不对,因为它从两个表返回相同的计数结果

SELECT count(m.id) as totmembers , count(c.id) as totcontracts
from members m
join contracts c on m.cid = c.id
where DATEDIFF(c.im_exp, CURDATE()) > 0

结果应该是 5000 份合同,12.000 名成员,但我得到了 12.000 个会员和 totcontracts。

4

2 回答 2

1

尝试这个:

SELECT count(m.id) as totmembers , count(distinct c.id) as totcontracts
from members m
join contracts c on m.cid = c.id
where DATEDIFF(c.im_exp, CURDATE()) > 0
于 2012-07-02T14:44:27.000 回答
0

因为您创建了一个 INNER JOIN 语句。在 SELECT 列表中创建一个包含两个单独查询的新查询。

SELECT (SELECT ... WHERE ...) AS totmembers, (SELECT ... WHERE ...) AS totcontracts
于 2012-07-02T14:30:51.657 回答