如何在mysql的where子句中使用max?

时间:2016-04-22 05:15:41

标签: php mysql

我有这个问题:

SELECT NAME, date, price 
    FROM purchase 
    WHERE Max(date) < '$lastweek' 
       AND NAME = '$customer' 
       GROUP BY NAME; 

查询如何查找最近的日期并检查具有给定日期的日期?

3 个答案:

答案 0 :(得分:1)

您可以尝试这样:

SELECT NAME, date, price 
    FROM purchase 
    WHERE (SELECT Max(date) FROM purchase) < '$lastweek' 
       AND NAME = '$customer' 
       GROUP BY NAME; 

答案 1 :(得分:0)

您是否尝试将SELECT MAX(date)作为子查询购买?

SELECT NAME, date, price 
    FROM purchase 
    WHERE (SELECT Max(date)) < $lastweek 

答案 2 :(得分:0)

你可以这样做:

SELECT name, MAX(date) as latest_date, price 
FROM purchase 
WHERE name= '$customer' 
GROUP BY name HAVING latest_date = '$date';