SQL - 表按事件排序

P. *_*ank 2 mysql sql

我有这样一张桌子:

+--------+--------+
| userid | events |
+--------+--------+
|   1    |  blog  |
|   2    |  blog  |
|   1    |  site  |
|   1    |  move  |
|   2    |  move  |
|   3    |  blog  |
|   2    |  blog  |
+--------+--------+
Run Code Online (Sandbox Code Playgroud)

我想显示参与我作为参数传递的所有事件的所有用户.

示例:参与"博客"和"移动"的所有用户

知道用户可以多次参加活动.

谢谢你的帮助

jue*_*n d 5

select userid
from your_table
where events in ('blog','move')
group by userid
having count(distinct events) = 2
Run Code Online (Sandbox Code Playgroud)

如果您要检查3个事件,请使用

having count(distinct events) = 3
Run Code Online (Sandbox Code Playgroud)

  • 如果你的表设计允许重复(OP似乎不是这种情况)那么只需使用`having count(distinct events)= 2` (2认同)