Jump to content


Photo

Return highest field number from query?


  • Please log in to reply
3 replies to this topic

#1 Prismatic

Prismatic
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego

Posted 26 September 2006 - 11:36 PM

Im having a big brain fart on this one  :P

I have a table setup like this

message_id | chat_id | user_id | user_name | message | post_time
233          |1          |10        |Bob          |Test        |2006-09-26 16:11:02
234          |1          |10        |Bob          |Weee      |2006-09-26 16:12:32
235          |1          |10        |Bob          |Arr!        |2006-09-26 16:14:56
etc..


What I need to do is run a query to grab the HIGHEST message_id in the table, anyone know what a query like that would look like? Sorry, im having a bad day heh

Thanks!




#2 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 26 September 2006 - 11:49 PM

Something like this should do it

$r = mysql_query("SELECT * FROM messages ORDER BY message_id DESC LIMIT 1") or die(mysql_error());


EDIT: Added limit
If you're not part of the solution, you're part of the precipitate

#3 High_-_Tek

High_-_Tek
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 27 September 2006 - 01:02 AM

"SELECT MAX(`message_id') as message_id FROM `TABLENAME`"

The MySQL-driven functions are VERY powerful

#4 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 27 September 2006 - 12:40 PM

Could even be simplified to:

$sql = "SELECT max(message_id) FROM tablename";

If you're just going to us an alias of exactly the same name, surely there's no point?

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users