mameha1977
10-18-2007, 07:46 PM
I want to select only the most recent record in the MySQL DB.
Is there some command to do that?
Is there some command to do that?
|
Click to See Complete Forum and Search --> : select most recent record only mameha1977 10-18-2007, 07:46 PM I want to select only the most recent record in the MySQL DB. Is there some command to do that? ryanbutler 10-18-2007, 08:23 PM I could be completely wrong here, little rusty on SQL but I believe you want the keyword LIMIT followed by a parameter, such as one. mameha1977 10-18-2007, 08:52 PM almost, it shows the first record though instead of the last one. Simon Moon 10-18-2007, 10:05 PM Multiple ways, depending on your table. Do you have a field for the date? A unique id field? If so, you can sort the table with that and use limit. Say you want only the latest entry and only that, the field name for your tables unique id is "recordid" then this query will get you the last entry from mysql: SELECT * FROM table ORDER BY recordid DESC LIMIT 1 Replace table with the actual name of your table of course. mameha1977 10-18-2007, 10:20 PM ok i just needed to add order by, thanks. i didnt think that would work if i am only selecting one record (ie. order by would apply to result data not all data). Simon Moon 10-18-2007, 10:27 PM ORDER BY is the whole data at all times. So if you use it on something large, make sure you got an index on that. mameha1977 10-18-2007, 11:38 PM thanks for the tip, ill check my bigger tables to make sure they got an index. mameha1977 10-18-2007, 11:52 PM the index should be on each field that you order by? or just any field is ok? webdeveloper.com
Copyright Internet.com Inc., All Rights Reserved. |