Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 执行这个复杂的SELECT查询的简单方法是什么?_Mysql_Sql_Performance - Fatal编程技术网

Mysql 执行这个复杂的SELECT查询的简单方法是什么?

Mysql 执行这个复杂的SELECT查询的简单方法是什么?,mysql,sql,performance,Mysql,Sql,Performance,在表格中给定这些条目: 我们需要处理一个子集条目,即数组[1,2,5,6] 问题: 查找具有相同项[1,2,5,6]及更多项的所有用户,例如[1,2,5,6,7]或[1,2,3,5,6]。 查找具有大量相同条目或更多条目的所有用户,例如[1,2,5,9]或[2,5,6,3]。 我能想到的第一个问题的最佳解决方案是以下select查询: SELECT DISTINCT user AS u FROM table WHERE EXISTS (SELECT * FROM table WHERE entr

在表格中给定这些条目:

我们需要处理一个子集条目,即数组[1,2,5,6]

问题:

查找具有相同项[1,2,5,6]及更多项的所有用户,例如[1,2,5,6,7]或[1,2,3,5,6]。 查找具有大量相同条目或更多条目的所有用户,例如[1,2,5,9]或[2,5,6,3]。 我能想到的第一个问题的最佳解决方案是以下select查询:

SELECT DISTINCT user AS u FROM table WHERE EXISTS (SELECT * FROM table WHERE entry=1 AND user=u)
                                    AND EXISTS(SELECT * FROM table WHERE entry=2 AND user=u)
                                    AND EXISTS(SELECT * FROM table WHERE entry=5 AND user=u)
                                    AND EXISTS(SELECT * FROM table WHERE entry=6 AND user=u)
另一方面,我感觉表面下潜藏着一些代数向量问题,特别是对于问题二,但我似乎无法集中注意力

欢迎所有想法

对于第一个问题:

SELECT user FROM (
    SELECT
    DISTINCT user
    FROM 
    table
    WHERE entry IN (1,2,5,6)
) a JOIN table b ON a.user = b.user
GROUP BY a.user
HAVING COUNT(*) >= 4
对于第二个问题,只需减少having子句中的计数。

对于第一个问题:

SELECT user FROM (
    SELECT
    DISTINCT user
    FROM 
    table
    WHERE entry IN (1,2,5,6)
) a JOIN table b ON a.user = b.user
GROUP BY a.user
HAVING COUNT(*) >= 4

对于第二个问题,只需减少having子句中的计数。

我认为执行此类查询的最简单方法是使用聚合和having。这里有一个例子

要得到正好包含这四个元素的A:

select user
from table
group by user
having sum(entry in (1,2,5,6)) > 0 and
       count(distinct entry) = 4;
要获得包含这四个元素以及其他元素的A:

select user
from table
group by user
having sum(entry in (1,2,5,6)) > 0 and
       count(distinct entry) >= 4;
要按用户拥有的匹配数和其他匹配数对用户进行排序,请执行以下操作:

select count(distinct case when entry in (1, 2, 5, 6) then entry end) as Matches,
       count(distinct case when entry not in (1, 2, 5, 6) then entry end) as Others,
       user
from table
group by user
order by Matches desc, Others;

我认为执行此类查询的最简单方法是使用聚合和查询。这里有一个例子

要得到正好包含这四个元素的A:

select user
from table
group by user
having sum(entry in (1,2,5,6)) > 0 and
       count(distinct entry) = 4;
要获得包含这四个元素以及其他元素的A:

select user
from table
group by user
having sum(entry in (1,2,5,6)) > 0 and
       count(distinct entry) >= 4;
要按用户拥有的匹配数和其他匹配数对用户进行排序,请执行以下操作:

select count(distinct case when entry in (1, 2, 5, 6) then entry end) as Matches,
       count(distinct case when entry not in (1, 2, 5, 6) then entry end) as Others,
       user
from table
group by user
order by Matches desc, Others;

这就是我对你的第一个问题的回答,尽管我认为Gordon Linoff的回答更有效:

select distinct user from so s1 
where not exists ( 
    select * from so s2 where s2.entry in (1,2,5,6) 
       and not exists ( 
          select * from so s3 where s2.entry = s3.entry and s1.user = s3.user
    )
);

对于第二个问题,您需要指定“很多”的含义。。。三,四

这是我对你的第一个问题的回答,尽管我认为Gordon Linoff的回答更有效:

select distinct user from so s1 
where not exists ( 
    select * from so s2 where s2.entry in (1,2,5,6) 
       and not exists ( 
          select * from so s3 where s2.entry = s3.entry and s1.user = s3.user
    )
);


对于第二个问题,您需要指定“很多”的含义。。。三,四

这看起来像是我正在寻找的简单而优雅的解决方案。当我确信它100%正确时,我会接受它。请注意,只有当组合用户条目是唯一的时,这才有效。否则,此查询还将返回具有例如四个值的条目的用户1@hage是的,我意识到了。我想这就是为什么fancyPants编辑了他的答案。不,hage的担心仍然有效。为了避免我提到的那些条目,您需要在SQL中模拟通用量词,没有显式操作。我发现的第一个链接看起来像是我正在寻找的简单而优雅的解决方案。当我确信它100%正确时,我会接受它。请注意,只有当组合用户条目是唯一的时,这才有效。否则,此查询还将返回具有例如四个值的条目的用户1@hage是的,我意识到了。我想这就是为什么fancyPants编辑了他的答案。不,hage的担心仍然有效。为了避免我提到的那些条目,您需要在SQL中模拟通用量词,没有显式操作。我找到的第一个链接谢谢你的最后一个查询。我没有要求它,但我需要一个非常类似的东西。我不知道你从哪里得到的,但是在1,2,5,6中有条目根本不起作用,因为只有聚合函数在having子句中起作用。@fancyPants。非常感谢。修复。看起来更好,是:+1感谢上次查询。我没有要求它,但我需要一个非常类似的东西。我不知道你从哪里得到的,但是在1,2,5,6中有条目根本不起作用,因为只有聚合函数在having子句中起作用。@fancyPants。非常感谢。修复。看起来更好,是:+1不。。。s1在第一行定义,内部查询知道它。内部查询不会提前执行。但是,对于最外层查询s1中的每一行,将执行内部块。。。s1在第一行定义,内部查询知道它。内部查询不会提前执行。然而,对于最外层查询s1中的每一行,将执行内部块。