Mysql: interesting ORDER/LIMIT question...

Discussion in 'MySQL' started by klemens_u, Oct 24, 2006.

  1. #1
    Hello!

    I've got a table called "names" like this:

    name, lastchangedate
    peter, 2005-02-07
    susa, 2003-01-23
    mike, 2006-10-24
    george, 2004-09-21
    angelina, 2001-02-03

    Now I want the latest 3 rows in ascending order. How do I do that?

    this query delivers the right 3 entries, but in the wrong order:
    "SELECT * FROM names order by lastchangedate desc limit 3;"

    this one is not an option because I don't know how many rows there are:
    "SELECT * FROM names order by lastchangedate limit 2,3;"

    Thank you for any suggestions!

    Have a nice day,

    Klemens / Vienna(Austria)
     
    klemens_u, Oct 24, 2006 IP
  2. infonote

    infonote Well-Known Member

    Messages:
    4,032
    Likes Received:
    68
    Best Answers:
    0
    Trophy Points:
    160
    #2
    You can first count the number of rows then use
    "SELECT * FROM names order by lastchangedate limit 2,3;"

    To count number of rows:

    dev.mysql.com/doc/refman/5.0/en/counting-rows.html
     
    infonote, Oct 24, 2006 IP
  3. klemens_u

    klemens_u Guest

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you infonote, but I'd like to have a solution in ONE query!
     
    klemens_u, Oct 24, 2006 IP
  4. rosytoes

    rosytoes Peon

    Messages:
    230
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    By latest, do you mean the latest according to the field lastchangedate? and by ascending, do you mean ascending in date or name order?
     
    rosytoes, Oct 24, 2006 IP
  5. klemens_u

    klemens_u Guest

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Yes, latest by date (lastchangedate), and ascending order of the date
     
    klemens_u, Oct 24, 2006 IP
  6. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #6
    "SELECT * FROM names ORDER BY lastchangedate ASC LIMIT 3;"
     
    php-lover, Oct 24, 2006 IP
  7. klemens_u

    klemens_u Guest

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    that returns the three rows with the oldest date, not the newest three!
     
    klemens_u, Oct 24, 2006 IP
  8. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #8
    IC....lets try this

    select * from(select * from names where lastchangedate < now() limit 3) as new order by lastchangedate asc;
     
    php-lover, Oct 24, 2006 IP
  9. rosytoes

    rosytoes Peon

    Messages:
    230
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #9
    create temporary table temp_names SELECT * FROM names ORDER BY lastchangedate DESC LIMIT 3;
    select * from temp_names order by lastchangedate;
    Code (markup):
     
    rosytoes, Oct 25, 2006 IP
  10. klemens_u

    klemens_u Guest

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    OK, thank you for the idea with the subselect. I'm still used to not having subselects from MySQL 3.x...

    That's what I finally used:
    select * from (select * from names order by lastchangedate desc limit 3) as new order by lastchangedate;
     
    klemens_u, Oct 26, 2006 IP
  11. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #11
    Ok...Klemens good luck :)
     
    php-lover, Oct 26, 2006 IP