我有所有的期权产品表可能有这样的(1种产品也可以有多种选择): products_id options_id在这种情况下有没有办法避免多重嵌套选择?
现在我需要选择具有ALL 3选项1,2,3所有products_ids那么我必须做这样的事情
select products_id
from options
where products_id in
(select products_id
from options
where products_id in
(select products_id
from options
where options_id = 3)
and options_id = 2)
and options_id = 1
这似乎是非常无效的,如果我要说10个选项。我不知道是否有一种方法来解决这一切
编辑: 我把一些样本数据的要求:
products_id options_id
1 1
1 2
1 3
1 4
1 5
2 1
2 2
2 3
3 2
3 3
3 9
4 1
4 2
4 8
如果我们用我上面的SQL,我们应该只产品1 ,其中2个具有所有选项1,2,3。其他人(3,4)不具备所有必需的选项
你能否提供'options'表的结构?我只是试图让你的查询返回多于0行。认真:) – 2012-03-27 08:22:10
我不知道如何发布表格数据在这里,但我会把一些示例数据的问题,所以你可以看到。请注意,1款产品可以有多个选项。 – mr1031011 2012-03-27 08:32:56
'请注意,1个产品可以有多个选项' - 好吧,然后试试这个'从选项中选择不同的products_id,其中options_id = 1 AND options_id = 2 AND options_id = 3' – 2012-03-27 08:34:33