我使用此查询获取最早的日期记录:
SELECT tt.*
FROM shop_meta tt
INNER JOIN
(SELECT seller_name, MIN(check_date) AS MinDateTime
FROM shop_meta
GROUP BY seller_name) groupedtt
ON tt.seller_name = groupedtt.seller_name
AND tt.check_date = groupedtt.MinDateTime
order by seller_name
我试图在特定日期之后获得所有最早的日期记录。
我尝试添加:
having MIN(check_date) > $date
having groupedtt.MinDateTime > $date
having tt.check_date > $date
having check_date > $date
和
where MIN(check_date) > $date
where groupedtt.MinDateTime > $date
where tt.check_date > $date
where check_date > $date
在查询中order by
之前,但我想我做错了。请帮忙!
编辑:
样本表:
seller_name status check_date
Seller 1 open 2016-05-09
Seller 1 open 2016-05-25
Seller 1 open 2016-05-26
Seller 2 open 2016-05-10
Seller 2 open 2016-05-20
Seller 3 open 2016-05-09
预期结果为$date = 2016-05-19
:
seller_name status check_date
Seller 1 open 2016-05-25
Seller 2 open 2016-05-20
答案 0 :(得分:2)
也许我错过了什么,但看起来很简单:
....
(SELECT seller_name, MIN(check_date) AS MinDateTime
FROM shop_meta
WHERE check_date > $date
GROUP BY seller_name) groupedtt
....
答案 1 :(得分:0)
SELECT tt.*
FROM shop_meta tt
INNER JOIN
(SELECT seller_name, MIN(check_date) AS MinDateTime
FROM shop_meta
GROUP BY seller_name
HAVING MIN(Check_date)>$date) groupedtt
ON tt.seller_name = groupedtt.seller_name
AND tt.check_date = groupedtt.MinDateTime order by seller_name