sql - where 语句取消了结果?
问题描述
我想UID
从下面的示例中找到符合自相矛盾的 where 陈述但为真的数字。这个想法是为了证明如果“父”和“关闭”在数据中,则“打开”和“子”不存在。下面的示例应UID=789
作为用例错误返回。
UID Title Business
123 Parent Open
123 Child 1 Open
123 Child 2 Open
456 Parent Closed
456 Child 1 Closed
456 Child 2 Closed
789 Parent Closed
789 Child 1 Open
789 Child 2 Closed
我一无所有,
select UID from TABLE
where
(TITLE = 'Parent' and Business = 'Closed')
and
(TITLE like 'Child%' and Business = 'Open')
解决方案
您可以聚合每个 UID 并用于HAVING
检查两种条件的记录是否存在:
select uid
from TABLE
group by uid
having count(case when title = 'Parent' and business = 'Closed' then 1 end) > 0
and count(case when title like 'Child%' and business = 'Open' then 1 end) > 0;
推荐阅读
- r - 如何为组中具有目标值的行设置子集,如果没有,则为同一组中的不同行设置子集?
- hibernate - 我可以在 entityManager.persist 时用其他 ID 覆盖 @SequenceGenerator 值吗
- arrays - 数组函数 VB
- ios - 嵌入式 UITextView 的圆角——什么是(错误地)保持圆角不透明?
- excel - 删除包含没有循环的单词的第 3 行
- android - 当应用程序最近时,GetIntent().getExtras() 通常为空 - Android
- git - 合并简单1提交分支的极简单git/Github用例导致分支删除失败
- r - 使用 data.table 和 lapply 的变量中的列名
- javascript - 将全局变量存储在单独的文件中以进行量角器测试
- php - 为什么我的登录页面在我点击登录按钮后转到 /login.php?login=error ?