不同记录的SQL查询

时间:2015-10-16 09:43:16

标签: mysql

这是我的查询

select  
    po.product_option_id,  
    po.product_id,  
    24 as option_id,  
    283 as option_value_id,  
    1 as quantity,  
    0 as subtract,  
    2.0000 as price,  
    '+' as price_prefix,  
    0 as points,  
    '+' as points_prefix,  
    0.00000000 as weight,  
    '+' as weight_prefix  
from oc_product_option po  
inner join oc_product_option_value pov  
on po.product_option_id = pov.product_option_id  
where po.option_id = 24  

我需要针对po.product_id的不同记录,现在它针对相同的product_id返回多条记录

任何建议都将不胜感激

1 个答案:

答案 0 :(得分:0)

使用mysql group by

group by po.product_id

将为您提供针对product_id的不同记录

select  
    po.product_option_id,  
    po.product_id,  
    24 as option_id,  
    283 as option_value_id,  
    1 as quantity,  
    0 as subtract,  
    2.0000 as price,  
    '+' as price_prefix,  
    0 as points,  
    '+' as points_prefix,  
    0.00000000 as weight,  
    '+' as weight_prefix  
from oc_product_option po  
inner join oc_product_option_value pov  
on po.product_option_id = pov.product_option_id  
where po.option_id = 24 group by po.product_id