Mysql 如何从订阅列表中获取仅订阅英语频道的用户
我有一张表格,上面有订阅英语和印地语频道的用户记录,我只想知道谁的用户订阅了英语频道Mysql 如何从订阅列表中获取仅订阅英语频道的用户,mysql,sql,hive,hiveql,Mysql,Sql,Hive,Hiveql,我有一张表格,上面有订阅英语和印地语频道的用户记录,我只想知道谁的用户订阅了英语频道 +--------+------+------+------+ | id | userid | Subscribedto | +--------+------+------+------+ | 1 | 1 | English | | 2 | 2 | English | | 3 | 1 | Hindi | | 4 | 3 |
+--------+------+------+------+
| 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'
)
您还可以将条件subscribedto='English'添加到where子句中:
结果是相同的,但此版本的效率可能稍高。假设订阅的列中没有空值,则可以使用“不存在”:
select t.*
from tablename t
where not exists (
select 1 from tablename
where userid = t.userid and subscribedto <> 'English'
)
您还可以将条件subscribedto='English'添加到where子句中:
结果是一样的,但是这个版本可能会稍微高效一些。分析函数将在单表扫描中完成这项工作,没有昂贵的联接。它将在蜂箱中工作:
SELECT id, userid, subscribedto
FROM
(
SELECT id, userid, subscribedto,
max(case when subscribedto != 'English' then true else false end) over(partition by userid ) subscribed_not_english
FROM my_table s
)s
WHERE NOT subscribed_not_english
分析函数将在单表扫描中完成此操作,无需昂贵的联接。它将在蜂箱中工作:
SELECT id, userid, subscribedto
FROM
(
SELECT id, userid, subscribedto,
max(case when subscribedto != 'English' then true else false end) over(partition by userid ) subscribed_not_english
FROM my_table s
)s
WHERE NOT subscribed_not_english
为什么结果表中没有id 1和id 4?他们还订阅了英语。您可以从您的“表格”名称中选择*,其中Subscribedto='English';我想你的意思是只订阅英语为什么结果表中没有id 1和id 4?他们还订阅了英语。您可以从您的“表格”名称中选择*,其中Subscribedto='English';我想你的意思是只订阅英语
SELECT id, userid, subscribedto
FROM
(
SELECT id, userid, subscribedto,
max(case when subscribedto != 'English' then true else false end) over(partition by userid ) subscribed_not_english
FROM my_table s
)s
WHERE NOT subscribed_not_english