多个在同一个表mysql中的位置?

时间:2014-12-03 10:42:18

标签: php mysql

我有一张名为' option_product'在MYSQL上

Id  id_product   id_option   id_value
1   1             34          77
2   2             34          74
3   1             2           12          
4   2             2           10
5   3             2           18

我想做那样的事情

select id from option_product where 

id_option = 34 and id_value = 77

and 

(id_option=2 and id value= 12 or id_option=2 and id value= 5)

我该怎么做?

查询应返回包含所有条件的id_product if(id_option = 34和id_value = 77)和(id_option = 2且id_value = 12或id_option = 2且id_value = 10)它将返回id_product = 1

3 个答案:

答案 0 :(得分:1)

Select id
From option_product
Where (id_option = 34 And id_value = 77) Or (id_option = 2 And id_value In (12, 5))

这将获得包含(option_id,id_value) - >的行。 (34,77),(2,12)和(2,5)

答案 1 :(得分:0)

我认为你的真正含义是:

select id from option_product where (id_option = 34 and id_value = 77)
OR (id_option=2 and id value= 12)
OR (id_option=2 and id value= 5)

答案 2 :(得分:0)

试试这个:

  select id from option_product where 
  (id_option = 34 and id_value = 77)
  or
  (id_option=2 and (id_value= 12 or id_value= 5) )