1

我有 2 张桌子

tableA has 41 rows

tableB has 3 rows

我正在尝试通过使用左连接的查询来获取这两个表的总行数,但我得到的行数(123)比预期的多(44)

询问:

SELECT COUNT(*)
    FROM tableA as u
LEFT JOIN tableB as d
    ON u.uid=d.uid
WHERE
    u.uid=912391178669
    AND
    u.deleted = 0
    AND
    d.deleted=0

表架构:

表A

编号 | uid | 已删除

表B

编号 | uid | 已删除

4

5 回答 5

2

我已经运行了以下查询它工作正常..你可以检查出来。

SELECT 
  ( SELECT count(*) from table1 where.... )
+ ( SELECT count(*) from table2 where.... )
as total from dual
于 2012-04-27T05:43:44.400 回答
1

我猜您在 tableA 中有三行,其中 uid 在查询中给出。这意味着 tableA 中的每一行将与 tableB 中的每一行连接一次,这意味着您将返回 41 x 3 行或 123。

从您期望返回的行数来看,我想知道您是否需要联合而不是联接。

Select * from tableA where uid = 912391178669 and deleted = 0

union all

Select * from tableB where uid = 912391178669 and deleted = 0

联合将结合两个查询的结果。连接将在单个查询中组合表表的列。

于 2012-04-27T05:00:03.500 回答
0
SELECT SUM(
  (SELECT count(*) from tableA WHERE uid=912391178669)
+ (SELECT count(*) from tableA WHERE uid=912391178669)
 ) as totalRows
于 2012-04-27T05:54:41.257 回答
0
41*3=123

TableA 的每一行都有 uid=912391178669 并且 tableB 的每一行也有 uid 这就是为什么你总共得到 123 行。使用一些过滤条件来获得所需的结果(如某些AND条件)

如果您可以向我们展示您的表格列,那么它可能会为您提供帮助。

左连接不合并两个表的行。
TableA 左连接 TableB 将为您提供满足连接条件的表 A 的所有行。

于 2012-04-27T05:00:34.460 回答
0
SELECT COUNT(*)
FROM tableA as u
LEFT JOIN tableB as d
ON u.uid=d.uid
AND
u.deleted = d.deleted
WHERE
u.uid=912391178669
AND u.deleted = 0
于 2012-04-27T05:37:30.430 回答