使用mysql查找两个列表之间的公共集

时间:2016-01-03 16:22:48

标签: mysql percentage set-difference

我有两个问题,我希望找到它们的共同价值观。我试图最终找出有多少用户访问过这两个网页。

SELECT DISTINCT user_id 
FROM table 
WHERE url ='y'
ORDER BY user_id;

SELECT DISTINCT user_id 
FROM table 
WHERE url ='z'
ORDER BY user_id;

我试过了

NOT IN 

UNION

但是没有多少运气 - 虽然我很容易做错了。我是新人。

1 个答案:

答案 0 :(得分:0)

一种方法是使用条件聚合。获取每个用户的信息:

select user_id,
       sum(url = 'y') as y_visits,
       sum(url = 'z') as z_visits
from t
group by user_id;

要获取用户列表,请添加having子句:

having y_visits >= 1 and z_visits >- 1

获取摘要信息:

select y_visitor, z_visitor, count(*)
from (select user_id,
             max(url = 'y') as y_visitor,
             max(url = 'z') as z_visitor
      from t
      group by user_id
     ) yz
group by y_visitor, z_visitor;

获得一个简单的百分比:

select avg(y_visitor = 1 and z_visitor = 1) as p_VisitedBothYandZ
from (select user_id,
             max(url = 'y') as y_visitor,
             max(url = 'z') as z_visitor
      from t
      group by url
     ) yz;