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
https://forums.phpfreaks.com/topic/204572-mysqlget-lastminimum-row-from-query/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.