mysql - 如何从订阅列表中获取仅订阅英语频道的用户
问题描述
我的表格包含订阅英语和印地语频道的用户记录,我只想要订阅英语的用户
+--------+------+------+------+
| id | userid | Subscribedto |
+--------+------+------+------+
| 1 | 1 | English |
| 2 | 2 | English |
| 3 | 1 | Hindi |
| 4 | 3 | English |
| 4 | 3 | Hindi |
| 5 | 4 | English |
+--------+------+------+------+
所以结果将是
+--------+------+------+------+
| id | userid | Subscribedto |
+--------+------+------+------+
| 2 | 2 | English |
| 5 | 4 | English |
+--------+------+------+------+
解决方案
假设列中没有空值,subscribedto
您可以使用 NOT EXISTS:
select t.*
from tablename t
where not exists (
select 1 from tablename
where userid = t.userid and subscribedto <> 'English'
)
请参阅演示。
结果:
| id | userid | subscribedto |
| --- | ------ | ------------ |
| 2 | 2 | English |
| 5 | 4 | English |
您还可以在 where 子句中添加条件subscribedto = 'English'
:
select t.*
from tablename t
where subscribedto = 'English' and not exists (
select 1 from tablename
where userid = t.userid and subscribedto <> 'English'
)
结果是一样的,但是这个版本可能会稍微高效一些。
推荐阅读
- android - 如何在没有钥匙的情况下获得额外物品?
- javascript - 如何在 reactjs 中添加状态小页面?
- python - Python 在我的路径中找不到 geckodriver
- c++ - 为什么这个模板代码不选择偏特化?
- ios - 如何在 Theos 中向 UIStatusBar 添加子视图?
- ios - CBCentralManager -- willRestore -- CBCentralManagerRestoredStateScanServicesKey
- excel - excel - sumifs 公式与标准范围
- mysql - AWS Glue 谓词下推条件无效
- python - 获取 Azure 容器位置 (python)
- android - 如何覆盖内部类中的全局变量