只允许三个新闻

I am creating a news feed and am trying to allow three news updates (per person) at a time.

The structure of the MYSQL db is as follows:

username|update1|update2|update3

bill     foo     bar     baz

I am wondering how I can make it so that when bill posts a new news update:

  • the value of update3 would be removed
  • the value from update2 would be moved to update3
  • the value from update1 would be moved to update2
  • and the new news update would be placed at update1

Would this be possible in simply a MYSQL query or would I have to use PHP to do this?

Try this:

UPDATE table
    SET update3 = update2,
        update2 = update1,
        update1 = {$your_input} 
    WHERE your_filter = your_condition

Instead of denormalizing the data like that, keep it pure:

timestamp|username|update

Then the last three updates can always be obtained with

select *
from updates
where username = 'the user'
order by timestamp descending
limit 3