mysql Query

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
leewad
Forum Commoner
Posts: 91
Joined: Tue May 11, 2004 8:32 am

mysql Query

Post by leewad »

Hi

I`m wanting to query my mysql database to list the latest records by that day, for example:

if today i update the database it will pull all todays listing but if not i want it to pull the last date range listing so if I did it last updated it friday it will pull these listings ( only these on this date not the ones before this), so basically I want to group the listing by the latest update.

Could anyone please help me with the query?
TeNDoLLA
Forum Newbie
Posts: 8
Joined: Wed Nov 12, 2008 3:52 pm

Re: mysql Query

Post by TeNDoLLA »

Could you post some code how you are trying to do it now? It is quite hard to guess how you db structure is designed and how you are trying to pull the data out of it at the moment.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: mysql Query

Post by aceconcepts »

Well in order to get the latest or earliest date you can use ORDER BY i.e.

Code: Select all

$query=mysql_query("SELECT * FROM table_name ORDER BY date_field");
To alternate the order use ASC or DESC respectively - ORDER BY defaults to ASC.
leewad
Forum Commoner
Posts: 91
Joined: Tue May 11, 2004 8:32 am

Re: mysql Query

Post by leewad »

Code: Select all

$query=mysql_query("SELECT * FROM table_name ORDER BY date_field");
This will list all records not all records on latest date.

If I have 10 records added on the 12-11-2008, 9 records added on the 08-11-2008 and 5 records added on the 06-11-2008 then I would like it to display the 10 records which were added on the 12-11-2008, if todays date was 10-11-2008 then it would only display the 9 records which were added on the 08-11-2008
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Re: mysql Query

Post by aaronhall »

You can pass a DATETIME, TIMESTAMP or DATE column into the MAX() function and figure out the latest date for which there are rows in the table
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: mysql Query

Post by aceconcepts »

Try and play around with it yourself - not being rude but you usually learn more when you research and test by trial and error :wink:
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: mysql Query

Post by pickle »

Since this is a MySQL Database question, I've moved it to the Databases forum.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply