0

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

我需要合并数据,以便我返回一个结果集,其中包括所有选项,以及用户选项,并以某种方式标记哪些仅限用户并且与主要选项数据重叠......

例子...

选项

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

用户选择的选项

`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,但我认为我需要通过将两张表连接在一起来做到这一点。到目前为止,我还没有提出解决方案。

4

2 回答 2

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

于 2013-03-02T00:11:36.017 回答
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 语句中以格式化信息:

(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)
于 2013-03-02T00:32:40.010 回答