mysql - 使用 SQL 对多个变体行进行价格搜索

标签 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 表的价格进行搜索时,我需要获取产品。

最佳答案

查找 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 

关于mysql - 使用 SQL 对多个变体行进行价格搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42159160/

相关文章:

java - Spring Boot MYSQL连接

MySQL 更新列,其序列号按具有相同值的字段分组

php - 在 SQL 查询中执行算术运算

c# - 考虑线程和并发请求生成自动编号

sql - 检查时间是否在两次之间(时间数据类型)

mysql - 重写查询以避免 not in 并提高性能

php - MySQL语句如果数字小于php变量

mysql - 轨道 4 : model relationships and indexing for two has_many through models

mysql - 选择对象中综合得票数前 10% 的集合

PHP 跳过重复项