使用sql在多个变量行上搜索价格

时间:2017-02-10 12:15:12

标签: mysql sql

我有一个低于价格表

Temp_attr table with different variant product with price

要求每个产品在单行中具有多个价格

我的SQL查询是

 select a.out_of_stack, product.*,
   ( select min(price) from temp_attr
     where product_id =product.product_id
   ) as min_price ,
   ( select max(price) from temp_attr
     where product_id = product.product_id
   ) as max_price from product
 left join users a
 on a.user_id = product.user_id
 where product.category_id in (37903,4707)
   and product.product_status in ('0')
   and product.draft=0
   and a.active=0
   and product.product_close=0
   and product.price between 10 and 4000
 group by product.product_id 

我需要在使用价格和temp_attr表进行搜索时获得该产品。

1 个答案:

答案 0 :(得分:0)

temp_attr中找到产品的最高/最低价格:

 SELECT MIN(price) , MAX(price) from temp_attr group by product_id 

加入productuser表:

  SELECT a.out_of_stack , product.*, t1.* FROM
  product
  left join (SELECT MIN(price) , MAX(price) from temp_attr group by product_id ) as t1
  on t1.product_id = product.product_id
  left join users as a
  on a.user_id = product.user_id
  where product.category_id in (37903,4707)
    and product.product_status in ('0')
    and product.draft=0
    and a.active=0
    and product.product_close=0
    and product.price between 10 and 4000
  group by product.product_id