我以这种方式在我的 whebshop 中存储我的产品价格:
product_id
price
_from: the date which the price is valid from
comment: a FK to a translation, its nullable
所以我得到这样的当前价格:
SELECT * FROM product_prices WHERE product_id = 1 AND _from < NOW() LIMIT 1
现在我想列出当前价格有评论的产品。 到目前为止,我做了这个查询:
SELECT
p.id,
pp.price
FROM
product_products p
JOIN product_prices pp
ON p.id = pp.product_id AND pp._from < NOW()
GROUP BY
p.id
HAVING
COUNT(pp.comment) > 0
这给了我所有有价格和评论的产品。理想情况下,我会为连接添加一个限制,不幸的是,这是不可能的:(我确定我需要一个子查询,但到目前为止我还想不出来。
欢迎提供任何帮助。
谢谢!
最佳答案
好的。我想我得到了解决方案。在这里:
SELECT
p.id,
pp.id,
pp.price,
pp.comment
FROM
product_products p
JOIN
product_prices pp
ON
pp.id = (SELECT id FROM product_prices WHERE _from < NOW() AND product_id = p.id ORDER BY _from DESC LIMIT 1)
WHERE pp.comment IS NOT NULL
GROUP BY
p.id
关于具有有效时间间隔的mysql价格处理,限制加入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20539858/