sql - 查找所有共享特定评论的记录
问题描述
我有一个现有的table1
:
User Comment Group
---------------------
1 foo a
2 bar a
3 baz b
4 123 a
5 bar c
6 foo d
7 654 a
假设我需要选择所有User
sComment
foo
select * from [table1] t1 where t1.[Comment] = 'foo';
User Comment Group
---------------------
1 foo a
6 foo d
现在我想找到同一个Group
s中的所有用户:
User Comment Group
---------------------
1 foo a
2 bar a
4 123 a
6 foo d
我怎样才能做到这一点?我是这样想的:
select t1.*
from [table1] t1
left join [table1] t2 on t1.[User] = t2.[User]
and t1.[Comment] = 'foo'
但这是返回所有记录[table1]
。我究竟做错了什么?
解决方案
你可以使用exists
:
select t.*
from mytable t
where exists (
select 1 from mytable t1 where t1.comment = 'foo' and t1.group = t.group
)
对于此查询的性能,您需要在(comment, group)
.
推荐阅读
- c# - 如何在 XML 中获取元素属性的值
- ios - RN App 在 testflight 审查中启动时崩溃,即使它适用于发布模式模拟器
- vue.js - 如何在使用 vue-test-utils 安装组件后触发 fetch 测试?
- r - 如何为将在 R Shiny 中执行的 R 脚本选择选项
- wso2 - 如何从 Okta 自定义 SAML AuthnRequest 以添加 AuthnContextDecl
- sql - 创建触发器以在从帐户到帐户的交易后更新余额 - SQL Oracle
- ubuntu - 如何根据热目录修复 btrfs 错误
- android - flutter_recaptcha_v2 依赖于 http ^0.12.0+1 而 my_app 依赖于 http ^0.13.1
- css - TailwindCSS 布局中的溢出问题
- android - 违反流异常透明度:检测到来自另一个协程的发射