我有一张包含以下内容的表格(我按日期排序):
ID prodId date
16 532 2015-08-17
19 535 2014-08-18
18 534 2011-08-17
27 48 2010-08-26
26 1541 2010-08-25
25 1541 2010-08-21
24 1540 2010-08-20
21 48 2010-08-19
20 48 2010-08-18
17 533 2010-08-17
14 532 2010-08-17
22 1540 1970-01-01
我想选择过去最近的prodId日期。我的问题是我得到了多个具有相同值的prodId(在这个例子中,48和1541)。
我的查询目前是:
SELECT * FROM `prods` WHERE (date <= '2010-08-26') order by date DESC
如何更改查询以删除不需要的行?
答案 0 :(得分:2)
SELECT * FROM prods p1
WHERE (date <= '2010-08-26')
AND Date in (Select Max(Date) from prods p2 where p1.prodId = pr.ProdId
and date <= '2010-08-26')
order by activeUntil DESC
答案 1 :(得分:0)
将limit 1
添加到查询
答案 2 :(得分:0)
您在寻找LIMIT声明吗?
SELECT * FROM `prods` WHERE (date <= '2010-08-26') order by activeUntil DESC LIMIT 1