SQL - 表按事件排序

时间:2015-10-08 09:58:48

标签: mysql sql

我有一张这样的表:

+--------+--------+
| userid | events |
+--------+--------+
|   1    |  blog  |
|   2    |  blog  |
|   1    |  site  |
|   1    |  move  |
|   2    |  move  |
|   3    |  blog  |
|   2    |  blog  |
+--------+--------+

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

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

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

感谢您的帮助

3 个答案:

答案 0 :(得分:5)

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

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

having count(distinct events) = 3

答案 1 :(得分:1)

SELECT DISTINCT userid
FROM table_name
WHERE events = 'blog'
AND userid IN
(
    SELECT DISTINCT userid
    FROM table_name
    WHERE events = 'move'
)

以下是SQLFiddle

答案 2 :(得分:0)

试试这个

select a.userid
from tablename a join tablename b
on a.userid = b.userid
where a.events='blog' and b.events='move';