简体   繁体   English

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

[英]How to do use max in where clause in mysql?

I have this query: 我有这个查询:

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

How will the query find the most recent date and check that date with the given date? 查询将如何找到最近的日期并用给定的日期检查该日期?

You can try like this: 您可以这样尝试:

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

Did you try SELECT MAX(date) from purchase as subquery ? 您是否尝试从购买中SELECT MAX(date)作为子查询?

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

You can do this: 你可以这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM