条件(在哪里)mysql

时间:2011-07-15 16:23:57

标签: mysql join where

表产品:

id|name
-------
1 |computer
2 |microwave
3 |transl

表product_features:

feature          | id_product | feature_value
------------------------------------
count_of_buttons | 1          | 1
count_of_buttons | 2          | 2
count_of_buttons | 3          | 1
color            | 1          | white
color            | 2          | white
color            | 3          | black

请问如何用一个按钮获得所有白色产品? 非常感谢你!

4 个答案:

答案 0 :(得分:4)

select product.*

from product
  join product_features as buttons
    on buttons.id_product = product.id
  join product_features as color
    on color.id_product = product.id

where buttons.feature_value = '1'
  and buttons.feature = 'count_of_buttons'

  and color.feature_value = 'white'
  and color.feature = 'color';

答案 1 :(得分:1)

select
    p.id
    p.name
from
    products p
    join (select * from product_features where feature = 'color') colors on (p.id=colors.id_product)
    join (select * from product_features where feature = 'count_of_buttons') buttons on (p.id=buttons.id_product)
where
    colors.feature_value = 'white'
    and buttons.feature_value = 1

您可以考虑重新组织product_features表,以便为每个要素(即color列和count_of_buttons列)分别设置一列,以便每个产品都有一行。事实上,它可能都在产品表中。

答案 2 :(得分:-1)

选择   p.id 来自产品p 加入product_features pf    ON p.id = pf.id 在哪里pf.feature_value ='white'AND pf.count_of_buttons = 1

答案 3 :(得分:-1)

select p.id, p.name from products p inner join product_features ON p.id=id_product where feature_value='white' and feature='color' and count_of_buttons=1