SQL help

They have: 49 posts

Joined: Dec 2001

Hey I think I've got a pretty easy question. How do you select only the most recent date from a table with SQL?

Hope someone can respond soon. Thanks to all

Krash_io

recipebomb.com

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

SELECT * FROM table_name ORDER BY field_name DESC LIMIT 1

Where field_name is your Timestamp/DateTime/Date data type field.

They have: 49 posts

Joined: Dec 2001

That seems to give me an error on LIMIT.

Any suggestions?

They have: 601 posts

Joined: Nov 2001

Try this:

SELECT * FROM table_name ORDER BY field_name DESC LIMIT 0,1
'

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

What kind of server are you using? (mySQL, M$ SQL, MS Access, etc.) There are some differences among these servers... the LIMIT query being one of them.

mySQL is what Will & I posted.
mySQL Docs: 6.4.1 SELECT Syntax

M$ SQL would look like this:
SELECT TOP 1 * FROM table_name ORDER BY field_name

M$ SQL Docs: SELECT

Mark Hensler
If there is no answer on Google, then there is no question.

They have: 49 posts

Joined: Dec 2001

Unfortunately I'm using Access

-Krash

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

It should work with the same query that MS SQL Server uses.

SELECT TOP 1 * FROM table_name ORDER BY field_name

They have: 49 posts

Joined: Dec 2001

Hey that worked great, but I needed to add a DESC to the end of that to get the news date, thanks for your help!

Krash_io

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.