SQL - 比较两组

时间:2013-11-06 09:15:33

标签: sql oracle

让我们说,我有员工,我知道他们喜欢什么水果。

fruits(name, fruit_name)

我的问题是:列出所有至少和唐纳德一样的成果的员工。

那么如何比较两组值?

这就是我得到唐纳德喜欢的成果的方式:

Select name, fruit_name
from fruits
where initcap(name) like '%Donald%';

示例:唐纳德喜欢苹果,梨,桃子。我需要那些喜欢苹果,梨,桃子和其他水果的人,但他们必须喜欢那些3。

6 个答案:

答案 0 :(得分:1)

您可以使用自我联接来获得所需的结果 - 我稍微调整了你的查询以获得输出 -

select distinct e1.name from fruits e1,(Select name, fruit_name
from fruits
where initcap(name) like '%Donald%') e2
where e1.fruit_name = e2.fruit_name;

上述查询返回至少一个水果与Donald匹配的员工

以下测试查询为员工提供至少所有唐纳德水果的匹配

     select name from (
    select name,count(1) cnt  from
    (select name,fruit_name, case when fruit_name in (Select distinct fruit_name
        from fruits
        where initcap(name) like '%Donald%') then 1 else 0 end fruit_match from fruits)
    where fruit_match = 1 group by name) where  cnt >=
 (select count(distinct fruit_name) from fruits where initcap(name) like '%Donald%');

答案 1 :(得分:1)

两种方法:

使用集合

我发现这提供了最易于理解的SQL,但确实需要定义集合类型:

CREATE TYPE VARCHAR2s_Table AS TABLE OF VARCHAR2(20);

然后,您可以将所有内容分组到集合中,并使用自联接和SUBMULTISET OF来查找其他名称。

WITH grouped AS (
  SELECT name,
         CAST( COLLECT( fruit ) AS VARCHAR2s_Table ) AS list_of_fruits
  FROM   fruits
  GROUP BY name
)
SELECT g.name
FROM   grouped f
       INNER JOIN
       grouped g
       ON (     f.list_of_fruits SUBMULTISET OF g.list_of_fruits
            AND f.name <> g.name )
WHERE  f.name = 'Alice';

SQLFIDDLE

或者替代版本:

WITH grouped AS (
  SELECT name,
        CAST( COLLECT( fruit ) AS VARCHAR2s_Table ) AS list_of_fruits
  FROM fruits
  GROUP BY name
)
SELECT name
FROM   grouped
WHERE  name <> 'Alice'
AND    ( SELECT list_of_fruits FROM grouped WHERE name = 'Alice' )
       SUBMULTISET OF list_of_fruits ;

不使用集合

WITH match_by_user AS (
  SELECT DISTINCT
         name,
         fruit
  FROM   fruits
  WHERE  name = 'Alice'
)
SELECT f.name
FROM   fruits f
       INNER JOIN
       match_by_user m
       ON (     f.fruit = m.fruit
            AND f.name  <> m.name )
GROUP BY f.name
HAVING COUNT( DISTINCT f.fruit ) = ( SELECT COUNT(1) FROM match_by_user );

SQLFIDDLE

顺便说一下 - 使用INITCAP(Name) LIKE '%Name%'有可能匹配多个名字,你可能会发现你正在找到几个人喜欢的水果。

答案 2 :(得分:0)

SELECT DISTINCT emp.name
FROM fruits emp
JOIN fruits don ON (don.fruit_name = emp.fruit_name)
WHERE INITCAP(fruits.name) LIKE '%Donald%';

这会让你得到所有员工的名字,这些员工至少有一种与唐纳德一样的水果,尽管你应该注意到你获得唐纳德喜欢的所有水果的解决方案,也会给你带来的结果,就像罗纳德麦当劳喜欢的那样。 / p>


我认为你需要所有喜欢唐纳德喜欢的所有水果的人。这更难。

SELECT DISTINCT emp.name
FROM employees emp
WHERE NOT EXISTS (
    SELECT *
    FROM fruits don_fruits
    LEFT JOIN fruits emp_fruits
        ON (don_fruits.fruit_name = emp_fruits.fruit_name AND emp_fruits.name = emp.name)
    WHERE INITCAP(don_fruits.name) LIKE '%Donald%'
      AND emp_fruits.name IS NULL
);

答案 3 :(得分:0)

在oracle plsql中,我会使用类似的东西来打印所有喜欢所有喜欢的水果的员工:

declare 
emp_name fruits.name%type;

begin

for rec in select distinct name from fruits
 loop

select name into emp_name from
(
 Select name, fruit_name
  from fruits
   where fruits.name=rec.name
 minus
   Select name, fruit_name
  from fruits
 where initcap(name) like '%Donald%'
)

if emp_name is null
 then
 dbms_output.put_line('Employee' || rec.name || 'likes same fruits as Donald');
end if

 end loop 

end

答案 4 :(得分:0)

试试这个:

Select distinct f2.name 
from fruits f1, fruits f2 where f1.fruit_name = f2_fruit_name 
where initcap(f1.name) like '%Donald%';

答案 5 :(得分:0)

select name
  from fruits
 where fruit_name in (select fruit_name
                        from fruits
                       where initcap(name) like '%Donald%'
                     )
   and initcap(name) not like '%Donald%'
 group by name
having count(fruit_name) = (select count(fruit_name)
                              from fruits
                             where initcap(name) like '%Donald%'
                            );