mysql检查最后x个帖子的userID

I have a php chat script on my site that I made, and I want to add in an anti-spam measure so that it wont post your message if the last 5 messages in the mysql table are by you.

Do I have to cycle through the last 5 with a recordset or is there a SQL statement that can just check this for me?

the table fields are just 'date' 'text' 'userid'

You can use a query like this to get how many of the last 5 messages where by the person in question.

select 
     sum(if(userid = '$user_id',1,0)) = 5 
     from (
         select userid from chat order by id desc limit 5
     ); 

In MySQL:

select userid, count(date) as howmany from 
( 
  select userid, date 
  from YOURTABLENAME 
  order by date desc
  limit 5
) 
  lastfiverows
group by userid
order by howmany

Then in PHP check to see if the "howmany" field is five, and the userid matches the user in question.

Order the record set (ORDER BY) by date (DESC) and LIMIT it to 5