联合两个查询并标记重复

时间:2013-03-02 00:05:31

标签: mysql sql join duplicates union

我有两张桌子。一个具有所有选项,一个具有用户选择的选项以及它们创建的一些选项可能不在选项表中。

我需要联合数据,所以我得到一个结果集,其中包括所有选项,以及用户选项和某种方式标记,它们只是用户,与主要选项数据重叠......

示例...

选项

`COLOR`   | `ANIMAL`
---------------------
RED       | DOG
BLUE      | DOG
GREEN     | DOG
YELLOW    | CAT
PINK      | CAT
ORANGE    | CAT

USER SELECTED OPTIONS

`COLOR` | `ANIMAL`
------------------
GREEN   | SNAKE
BLUE    | DOG
PINK    | CAT
PURPLE  | CAT

我的结果需要看起来像......

`COLOR` | `ANIMAL`| `DUPLICATE_OR_NEW`
----------------------------------------
RED     | DOG     | 0
BLUE    | DOG     | 1
GREEN   | DOG     | 0
YELLOW  | CAT     | 0
PINK    | CAT     | 1
ORANGE  | CAT     | 0
PURPLE  | CAT     | 1
GREEN   | SNAKE   | 1

在这种情况下,排序顺序无关紧要。我正在尝试使用UNIONS,但我认为我需要将两个表连接在一起。到目前为止,我还没有提出解决方案。

2 个答案:

答案 0 :(得分:0)

这可能被称为作弊但它会起作用

SELECT COLOR, ANIMAL, sum(DUPLICATE_OR_NEW)
FROM
(
SELECT COLOR, ANIMAL, 1 as DUPLICATE_OR_NEW FROM options
UNION ALL
SELECT COLOR, ANIMAL, 2 as DUPLICATE_OR_NEW FROM UserSelection
) as UTable
GROUP BY  COLOR, ANIMAL

-- 1 = unchosen option
-- 2 = new user added option
-- 3 = option exsisted chosen by user

请参阅SQL Fiddle http://sqlfiddle.com/#!2/01c79/2

答案 1 :(得分:0)

另一种解决方法:

select color, animal, 1 as duplicate_or_new
from UserSelected
union all
select color, animal, 0 as duplicate_or_new
from options o
where not exists (select 1 from UserSelected us where us.color = o.color and us.animal = o.animal)

使用union all / group执行此操作的正确方法:

 select color, animal, max(which) as duplicate_or_new
 from (select color, animal, 1 as which
       from UserSelected
       union all
       select color, animal, 0 as which
       from options
      ) t
group by color, animal

以下查询创建两个单独的标志:

 select color, animal, max(isUser) as IsUser, max(isOption) as IsOption
 from (select color, animal, 1 as IsUser, 0 as IsOption
       from UserSelected
       union all
       select color, animal, 0 as IsUser, 1 as IsOption
       from options
      ) t
group by color, animal

您可以将它们放在案例陈述中以格式化信息:

(case when max(isUser) = 1 and max(isOption) = 1 then 'both'
      when max(isUser) = 1 then 'user'
      when max(isOption) = 1 then 'option'
      else 'impossible'
 end)