- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我在 PostgreSQL 中有三个表:
table1 - id1, a_id, updated_by_id
table2 - id2, a_id, updated_by_id
Users - id, display_name
假设我正在使用以下查询:
select count(t1.id1) from table1 t1
left join table2 t2 on (t1.a_id=t2.a_id)
full outer join users u1 t1.updated_by_id=u1.id)
full outer join users u2 t2.updated_by_id=u2.id)
where u1.id=100;
我的计数是 50
。
鉴于:
select count(t1.id1) from table1 t1
left join table2 t2 on (t1.a_id=t2.a_id)
full outer join users u1 t1.updated_by_id=u1.id)
full outer join users u2 t2.updated_by_id=u2.id)
where u2.id=100;
我只得到 25
作为计数。
我在第二个查询中的错误是什么?我该怎么做才能获得相同的计数?
我的要求是有一个用户表,被多个表引用。我想获取完整的用户列表并从不同的表中获取 ID 的数量。
但我单独加入的表返回正确的计数,但其余表不返回正确的计数。有人可以建议一种方法来修改我的第二个查询以获得正确的计数吗?
最佳答案
为了简化您的逻辑,先聚合,后加入。
猜测缺失的详细信息,此查询将为您提供准确的计数,即每个用户分别在 table1
和 table2
中被引用的次数 所有用户:
SELECT *
FROM users u
LEFT JOIN (
SELECT updated_by_id AS id, count(*) AS t1_ct
FROM table1
GROUP BY 1
) t1 USING (id)
LEFT JOIN (
SELECT updated_by_id AS id, count(*) AS t2_ct
FROM table2
GROUP BY 1
) t2 USING (id);
特别是,避免多个 1-n 关系在连接在一起时相互相乘:
要仅检索单个或几个用户,LATERAL
连接会更快(Postgres 9.3+):
SELECT *
FROM users u
LEFT JOIN LATERAL (
SELECT count(*) AS t1_ct
FROM table1
WHERE updated_by_id = u.id
) ON true
LEFT JOIN LATERAL (
SELECT count(*) AS t2_ct
FROM table2
WHERE updated_by_id = u.id
) ON true
WHERE u.id = 100;
您报告的特定不匹配是由于 FULL OUTER JOIN
的特殊性造成的:
First, an inner join is performed. Then, for each row in T1 that doesnot satisfy the join condition with any row in T2, a joined row isadded with null values in columns of T2. Also, for each row of T2 thatdoes not satisfy the join condition with any row in T1, a joined rowwith null values in the columns of T1 is added.
因此,您会在另一侧附加 NULL 值以表示缺少匹配项。 count()
不计算 NULL 值。因此,根据过滤 u1.id=100
还是 u2.id=100
,您可以获得不同的结果。
这只是为了说明,这里不需要FULL JOIN
。请改用提供的替代方案。
关于sql - 在 PostgreSQL 中加入三个表后计算行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32260564/
我似乎对 git 存储库有权限问题。 当我 pull 入一个不是我的 Linux 用户创建的目录时,我出现了这个错误。 fatal: Unable to create '/home/---/.git/
在 Git 中,您可以将给定目录克隆到给定目录: git clone ssh://gitolite@dev.bipper.com:3687/com/bipper/kids/portal 当我运行我们
目前,如果您在分支 V2 中并执行“git pull origin V3”,它会将 V3 merge 到 V2,甚至不会发出警告或提示。这个选项可以以某种方式被阻止吗?我在这里阅读了所有类似的问题,人
我刚开始使用 Oracle 的 Coherence 缓存,我注意到这一点:如果我在缓存中放入一个 ConcurrentHashMap 对象,当我检索它时,我可以看到它被转换为一个普通的 HashMap
看起来我缺少对 git pull 和 git commit 的基本理解,假设我在分支上工作,而它在我更新时被其他开发人员更新了在本地做我的工作。我应该在发出 git pull 之前提交更改,还是应该执
好的。所以我以为我已经舔过了……但现在…… 我有一个项目,其中包含一个来自 GitHub 的小型库作为子模块。在该 super 项目的原始版本中,子模块按预期工作。 但是,我只是克隆了 super 项
使用 Visual Studio Code 中的内置 Git,我看不到将指定的远程分支 pull 入当前分支的方法。我可以这样做吗? 示例:我正在分支 myBranch 上工作,更改已 merge 到
当我尝试提交或 pull 此错误时 Bus error (core dumped) 发生了! 当我用 gdb 调试它时,(gdb git,run commit -a,where) 结果是: mucul
我对默认 Rails Rake 任务的预期用途有点困惑,想咨询一下我是否应该使用 db:reset或编写自定义 Rake 任务。没什么聪明的,只是日常管理,而且我很可能会错过一个明显的文档,因为我是
所以我做了: git reset --hard #commithash # make a bunch of changes, fixes and so on. git add -A git commi
我已使用以下命令成功部署到 firebase 托管应用: firebase init firebase deploy 在这个阶段,我正在执行 git pull 以将 repo 下 pull 到暂存服务
当尝试在 Eclipse 的 git 存储库中 pull (团队|从上下文菜单中 pull )时,出现 Could not get advertised Ref for branch refs/hea
我是一名优秀的程序员,十分优秀!