我有一个似乎无法解决的问题。我有一个跟踪userID和app ID的表。该表如下所示:
Library:
ID userID appID
1 1 11122
2 1 65324
3 1 43435
4 2 43435
5 2 50645
6 2 34343
7 4 11122
8 4 55343
查询应执行以下操作:
这是我正在尝试做的PHP示例:
$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
结果将是“绿色”和“红色”
这可以通过MySQL吗?
答案 0 :(得分:0)
我不是肯定的我完全理解你的问题,但听起来你想要返回用户ID 1和2,因为它们与相同的appID相关联(实际上是两次)。如果是这样,那么这应该有效:
select L.userID, L.appID
from Library L
join (
select appID
from Library
group by appId
having count(distinct userID) > 1
) L2 on L.appID = L2.appID
如果您只需要唯一的userID,请从上面的查询中删除appID并添加DISTINCT。
答案 1 :(得分:0)
你可以试试这个:
select a.appID from Library a
inner join
(select appID,count(ID) as ctr from Library where userID in(1,4) group by appID) b on b.appID = a.appID
where b.ctr > 1
group by a.appID;
<强>更新强>
我不确定您是否只想将普通用户ID查看为1列
select a.appID,group_concat(a.userID) as user_ids from Library a
inner join
(select appID,count(ID) as ctr from Library where userID in(1,4) group by appID) b on b.appID = a.appID
where b.ctr > 1
group by a.appID;