Alex
Alex

Reputation: 1659

Select the ROW having the Maximum Value of a certain Column

For example, if my query returns two rows or more, I would like to select the row having the most recent date.

I'm doing something like this:

SELECT * FROM Table1 WHERE Name=Mark AND MAX(TIMESTAMP(date(str_to_date(DATE_REGISTERED,'%d/%m/%Y'))))

The error returned is: #1111 - Invalid use of group function

Upvotes: 0

Views: 132

Answers (2)

Ken Herbert
Ken Herbert

Reputation: 5236

To limit your results to only the most recent (based on a field named DATE_REGISTERED as per your example) you would do

SELECT * FROM Table1 WHERE Name='Mark' ORDER BY STR_TO_DATE(DATE_REGISTERED,'%d/%m/%Y')  DESC LIMIT 1

Upvotes: 2

Rachid O
Rachid O

Reputation: 14032

you should use the order by statement:

SELECT * FROM Table1 WHERE Name=Mark
order by str_to_date(DATE_REGISTERED,'%d/%m/%Y') DESC LIMIT 1

Upvotes: 1

Related Questions