Jump to content

[mysql]get last/minimum row from query.


madmenyo

Recommended Posts

Hi,

 

I'm kinda stuck on a query. I have a table for messages with a timestamp field. Now i want to store a maximum amount of messages in that table, so each time a message is entered it must check the table if the num_rows > $maximum.

 

If that is true lookup the timestamp of the latest message of the last $maximum messages. Get that in a variable or array so i can delete everything older from the table.

 

Now i can de a query like this:

$lastmsg = mysql_query ("
		SELECT time
		FROM chat
		ORDER BY time DESC	
		LIMIT $store_num
") or die (mysql_error());

 

And loop through the query, check the oldest timestamp each loop and if it's older then put that into a variable. But obviously i already know it's the last table :D. I would love to do something like this:

$lastmsg = mysql_query ("
		SELECT LAST(time)
		FROM chat
		ORDER BY time DESC 
		LIMIT $store_num <---- this needs priority
") or die (mysql_error());

 

But like SELECT MIN(time) it would just select the minimum in the whole table and then LIMIT becomes obviously useless unless there are more rows with the same minimum in the entire table.

 

So can i build a query to select 1 row out of many rows and how to do that?

 

tx!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.