3

Can anyone tell me why I'm getting Invalid use of group function and how to stop it?

SELECT Name, Message
FROM flux_chat_messages
WHERE id >= ( MAX( id ) -5 )
ORDER BY id ASC
OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
CyanPrime
  • 5,096
  • 12
  • 58
  • 79

1 Answers1

11

You cannot use MAX() in a WHERE. So wrap it in a subquery like:

SELECT Name, Message
FROM flux_chat_messages
WHERE id >= (SELECT MAX( id ) - 5 FROM flux_chat_messages)
ORDER BY id ASC

Also probably you could have

SELECT Name, Message
FROM flux_chat_messages
ORDER BY id DESC
LIMIT 5

and reverse the results in your program (or use another subquery for that)

zerkms
  • 249,484
  • 69
  • 436
  • 539
  • Ah, I had to select the max(id) -5 to turn it into a useable number. Thank you. I'll accept when I can :) – CyanPrime Mar 12 '12 at 04:26