简体   繁体   中英

In PHP, how do I ask a mySQL database to retrieve the most recent record that matches a certain value?

I have a table where the user will select a category and enter a search parameter, and I have to find the latest value in hat category.

An example of a dataset might be:

ID  | category  | name  | price | date
1   | bubblegum | Steve | 2.00  | 2011/1/1
2   | bubblegum | Bob   | 1.00  | 2011/1/8
3   | bubblegum | Steve | 1.80  | 2011/2/1

If the user searched for "Steve", they would get only the latest price that also has the name "steve" in it.

Max(date) doesn't seem to work though, and I don't understand why. I think it is because if, were I to look for "bob", because Bob doesn't appear in the Max(date), it won't show up. But maybe I am using the SELECT parameter wrong.

SELECT *
FROM yourtable
ORDER BY date DESC
WHERE name='Steve'
LIMIT 1
SELECT * FROM yourtable WHERE name='Steve' ORDER BY date DESC LIMIT 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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