What is the best way to organize my data by 2 columns?

Discussion in 'PHP' started by Imozeb, Apr 30, 2010.

  1. #1
    I am querying data from a database through PHP. One column contains date values and another column contains interger values ranging from 1-99. How would I create a query or a function that will get 25 of the most recent dates, and then sort them by the second column (1-99), and then output just 4 of them.

    Thanks.
     
    Imozeb, Apr 30, 2010 IP
  2. Belthasar

    Belthasar Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You could do something like in mysql.

    SELECT * FROM table ORDER BY dates_table DESC, int_table ASC LIMIT 4
     
    Belthasar, Apr 30, 2010 IP
  3. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    But won't your query just get the four most recent dates and order them by the second column? I need a way to get 25 of the most recent dates ordering them by
    most recent and then by the second column and then outputing just 4. If there is no wAy inmysql to do this how do you think I could do it using php?
    Thanks!
     
    Imozeb, May 1, 2010 IP
  4. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Does anyone have any idea how I could do this?
     
    Imozeb, May 2, 2010 IP
  5. s.ham

    s.ham Member

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #5
    select * from (SELECT * FROM table ORDER BY date_column DESC LIMIT 4) a
    order by a.numeric_column asc;

    This might work. Check it out.
     
    s.ham, May 2, 2010 IP