0

我有两个表:DATA 和 USERS

用户

id       sqft       postal       province       city
==========================================================
1          1          Y7R          BC          Vancouver
2          2          Y7R          BC          Vancouver
3          1          L5B          ON          Toronto

数据

id     uid     power
=======================
1       1       1000
2       2       1300
3       1       1500

表 DATA 中的 uid 与表 USERS 中的 id 匹配

我希望能够计算 DATA 中不同 uid 的数量,postal code其中Y7R1sqft

4

4 回答 4

0

Try this one:

SELECT COUNT(DISTINCT a.id)
  FROM   USERS a
            INNER JOIN  DATA b 
                ON a.id = b.uid
 WHERE  a.sqft = 1 AND
        a.postal = 'Y7R'
于 2012-07-15T01:17:53.990 回答
0
SELECT COUNT(id) 
   FROM `DATA` AS `d` 
   INNER JOIN `USERS` AS `u` 
   ON u.id=d.uid 
   WHERE u.postal='Y7R' AND u.sqft=1 
   GROUP BY u.id;

如果您有适当的模式,它们无论如何都应该是不同的,如果是这样,只需删除 group by 子句。

于 2012-07-15T00:08:29.750 回答
0
SELECT COUNT(DISTINCT D.UID) FROM DATA D 
LEFT JOIN USERS U ON D.UID=U.ID 
WHERE U.POSTAL='Y7R' AND U.SQFT=1)

如果您需要不同的

于 2012-07-15T00:09:42.110 回答
0

您可以使用此解决方案:

SELECT COUNT(DISTINCT a.id)
FROM   USERS a
JOIN   DATA b ON a.id = b.uid
WHERE  a.sqft = 1 AND
       a.postal = 'Y7R'
于 2012-07-15T00:14:02.243 回答