0

这是我的以下情况:

表:选择

selection_id | group_id | selection_group_id |  
------------------------------------------------
     1            1              1

表:选择器

selector_id | selection_id | name | index
------------------------------------------
     1             1         Step1   0 
     2             1         Step2   1 
     3             1         Step3   2

我的问题:如何找出选择器的唯一“selection_id”以及构造选择单元的联合信息“Step1,Step2,Step3”?

我试图更准确地描述它:我需要一个例程,它从与相同的“selection_id”连接的 3 个选择器中获取唯一的“selection_id”。我的信息是选择器的“名称”和“索引”。“选择器”的名称可能经常出现,但选择器的连续索引是唯一的。——</p>

4

3 回答 3

0

我想你想使用GROUP_CONCAT- 不确定你甚至需要JOIN你的桌子:

SELECT selection_id, GROUP_CONCAT(name)
FROM selectors

这是SQL 小提琴

并找出唯一的 selection_id:

SELECT DISTINCT Selection_ID
FROM (
  SELECT selection_id, GROUP_CONCAT(name) gc
  FROM selectors
  ) t
WHERE gc = 'Step1,Step2,Step3'

http://sqlfiddle.com/#!2/4609f/13

如果我误解了你的问题,请告诉我。

祝你好运。

于 2013-02-05T01:28:10.893 回答
0

为此,您需要使用having子句进行聚合。例如:

select selector_id
from selectors s
group by selector_id
having max(case when name = 'Step1' then 1 else 0 end) = 1 and
       max(case when name = 'Step2' then 1 else 0 end) = 1 and
       max(case when name = 'Step3' then 1 else 0 end) = 1 and
       max(case when name in ('Step1', 'Step2', 'Step3') then 0 else 1 end) = 0

我发现这种方法是最通用的。. . having根据条件最大值使用具有各种条件的语句。还有其他一些表达方式,例如:

select selector_id
from selectors s
group by selector_id
having count(distinct name) = 3 and
       max(case when name in ('Step1', 'Step2', 'Step3') then 0 else 1 end) = 0

看起来更简单,但不是一般的形式。

于 2013-02-05T01:46:45.083 回答
0

我已经建立了我的解决方案:http ://sqlfiddle.com/#!2/fdbba/20/0

SELECT selections.selection_id FROM `prstshp_productsort_groups_selections` selections 
WHERE EXISTS (SELECT * FROM  `prstshp_productsort_groups_selectors` s3
                WHERE s3.name = "Muskelaufbau" AND s3.index = 0 AND s3.selection_id = selections.selection_id)
AND
EXISTS (SELECT * FROM  `prstshp_productsort_groups_selectors` s4
                WHERE s4.name = "Mesomorph" AND s4.index = 1 AND s4.selection_id = selections.selection_id)
AND
EXISTS (SELECT * FROM  `prstshp_productsort_groups_selectors` s5
                WHERE s5.name = "Männlich" AND s5.index = 2 AND s5.selection_id = selections.selection_id)
AND
EXISTS (SELECT * FROM  `prstshp_productsort_groups_selectors` s6
                WHERE s6.name = "25-40" AND s6.index = 3 AND s6.selection_id = selections.selection_id)
于 2013-02-05T14:07:08.990 回答