Nasir Hussain
Nasir Hussain

Reputation: 139

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?

Upvotes: 0

Views: 1344

Answers (3)

Sarfaraz Khan
Sarfaraz Khan

Reputation: 9

You can do this:

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

Upvotes: 0

Tony Vincent
Tony Vincent

Reputation: 14262

Did you try SELECT MAX(date)from purchase as subquery ?

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

Upvotes: 0

Md Mahfuzur Rahman
Md Mahfuzur Rahman

Reputation: 2359

You can try like this:

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

Upvotes: 1

Related Questions