我试图获得某些物品的最低价格 - 工作。 问题是我不仅需要最低价格,还要考虑以下条件:
我尝试了HAVING和CASE WHEN,但我认为这是废话。
所以下面数据的正确答案是
.:/usr/lib/php:/usr/local/lib/php
3, 10, 500
5, 20, 410
7, 30, 110
Table
pid, oid, price, date
1, 10, 300, 2013-09-01
2, 10, 400, 2014-09-01
3, 10, 500, 2016-09-01
4, 20, 310, 2013-10-01
5, 20, 410, 2017-10-01
6, 20, 510, 2018-10-01
7, 30, 110, 2012-10-01
8, 40, 120, 2013-10-01
答案 0 :(得分:1)
如下所示:
SELECT p.oid, COALESCE(fp.price, pp.price) AS price
FROM (SELECT DISTINCT oid FROM prices) p
LEFT JOIN (
SELECT oid, min(price) AS price
FROM prices
WHERE date>=CURDATE()
GROUP BY oid) fp
ON p.oid=fp.oid
LEFT JOIN (
SELECT oid, min(price) AS price
FROM prices
WHERE date<CURDATE()
GROUP BY oid) pp
ON p.oid=pp.oid