Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2002

    Question Unanswered: simple sql query - help please!

    Hello everybody.

    I have a table that contains id, name, message, and date.
    I would like to pull the 10 last posted messages, but have a hard time doing it. I would also like to take into consideration that ids might not be consecutive as some messages get deleted.

    Maybe I am doing it wrong.

    This is what I am trying with no success:

    select * from messages where id > max(id)-10;

    this gives me a ERROR 1111: Invalid use of group function.

    Can anyone please please suggest what I am not doing right?

    I also tried different variations of the above query with disaster... =(

    Thanks a bunch.

  2. #2
    Join Date
    Mar 2002

    what i tried:

    Right now I get by using one query for getting the max(id), then doing another query for all messages whose ids are greated than int num = max(id) -10;

    anne b.

  3. #3
    Join Date
    Feb 2001
    NC, USA
    When using the max() function or any other aggregation function, you must include a group by clause.

    To get the latest 10 posts you can use the LIMIT clause.
    SELECT * FROM messages ORDER BY date DESC LIMIT 10;

  4. #4
    Join Date
    Mar 2002
    Hi Anne,

    I belive u cant' get max(of any column) directly. Yu need to fire the query for it.

    Acc. to me it should be, select * from tablename where id > (select max(id) from tablename -10.

    But this wont work in mysql as it doesnt support nested querries...

    This is what I belive, If i am wrong...correct me please.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts