【问题标题】:MySQL Find minimum price row for multiple same productMySQL查找多个相同产品的最低价格行
【发布时间】:2018-05-14 18:15:22
【问题描述】:

我有许多相同产品但条件不同和不同供应商/供应商的产品表,我想获得价格最便宜的产品以及其他数据。

这里有一些条目

+----------+--------------------+-----------+------------+------------+--------------+-------------+
|    ID    |    Product Name    | new_price | old_price  |    price   |  provider_id |  condition  |
+----------+--------------------+-----------+------------+------------+--------------+-------------+
|    1     |   samsung tv 32    |   1200    |    null    |    null    |       2      |     new     |
|    2     |   samsung tv 32    |    null   |    null    |     800    |      123     | refurbished |
|    23    |   samsung tv 32    |    null   |     300    |    null    |       6      |     used    |
|    48    |   samsung tv 32    |   1500    |    null    |    null    |       8      |     new     |
|    2     |    smart watch     |    null   |    null    |     200    |      123     | refurbished |
|    23    |    smart watch     |    null   |     100    |    null    |       6      |     used    |
+----------+--------------------+-----------+------------+------------+--------------+-------------+

想要的结果

+----------+--------------------+-----------+--------------+-------------+
|    ID    |    Product Name    |   price   |  provider_id |  condition  |
+----------+--------------------+-----------+--------------+-------------+
|    23    |   samsung tv 32    |    300    |       6      |     used    |
|    23    |    smart watch     |    100    |       6      |     used    |
+----------+--------------------+-----------+--------------+-------------+

这是我的问题,我累了。

SELECT
    MIN(
        IF(
            new_price > 0,
            new_price,
            IF(
                old_price > 0,
                old_price,
                IF(
                    price > 0,
                    price,
                    0
                )
            )
        )
    ) AS price,
    `name`,
    `id`,
    `provider_id`
FROM
    `products`
GROUP BY
    `name`

【问题讨论】:

  • 您能否提出查询以使table..
  • SELECT * FROM my_table WHERE id = 23

标签: mysql aggregate


【解决方案1】:

要获得所需的结果集,您可以使用带有coalesce 的自联接来获得第一个非空值。

select p.ID,
p.name,
coalesce(p.`new_price`, p.`old_price`, p.`price`) as price,
p.provider_id,
p.condition
from products p
left join products p1 on p.name  = p1.name 
and coalesce(p.`new_price`, p.`old_price`, p.`price`) > coalesce(p1.`new_price`, p1.`old_price`, p1.`price`)  
where p1.ID is null

Demo

使用 join 获得相同结果的另一种方法

select p.ID,
p.name,
coalesce(p.`new_price`, p.`old_price`, p.`price`) as price,
p.provider_id,
p.condition
from products p
join (
  select name, min(coalesce(`new_price`, `old_price`,`price`)) as price
  from products
  group by name
) p1 on p.name  = p1.name 
and coalesce(p.`new_price`, p.`old_price`, p.`price`) = p1.price

Demo

【讨论】:

  • 您能解释一下p1.id 是如何为空的吗?
  • 您可以看到左连接部分some value > some value,在您的情况下,当第一个表 p 的价格 300 不大于第二个表 p1 的任何价格时,您将获得 p1 的空行,在将那些 p1 为空值的行缩短为您所需的价格最便宜的行,有关更多说明,请参阅小提琴演示 sqlfiddle.com/#!9/a7eb20/6 和这个也是 sqlfiddle.com/#!9/a7eb20/7
  • 哇,非常感谢您的精彩解释。我在我的实际数据库中写问题时也犯了一个错误,我的数据库中为零而不是空。那么在这种情况下我应该怎么做,我应该修改类似coalesce(if(p.new_price=0,null, p.new_price), if(p.old_price=0,null, p.old_price), if(p.price=0,null, p.price)) as price 的内容还是有其他方法?
  • @HumairaNaz 你可以使用GREATEST() 函数,如greatest(p.new_price, p.old_price, p.price)
  • 还有一个问题,如果你能接受这个。如何使用 laravel eloquent 模型进行此查询?我很困惑我应该如何在 eloquent 中编写连接查询。
猜你喜欢
  • 2017-12-05
  • 1970-01-01
  • 2016-04-25
  • 2016-05-09
  • 2020-10-16
  • 2013-01-14
  • 1970-01-01
  • 1970-01-01
  • 2015-11-05
相关资源
最近更新 更多