How to sort by previous date in database?

后端 未结 4 992
孤街浪徒
孤街浪徒 2021-01-16 07:46

What I want to ask is how to select from database record and my php code able to sort previous date by date,which is what i want to display in my interface:



        
相关标签:
4条回答
  • 2021-01-16 07:57

    Can't you simply use this?

    SELECT `date` FROM staff WHERE `date` < NOW() ORDER BY `date`
    

    or (if you need only different dates)

    SELECT DISTINCT `date` FROM staff WHERE `date` < NOW() ORDER BY `date`
    
    0 讨论(0)
  • 2021-01-16 07:58

    the code you need is a very simple one.
    you just have to understand the meaning of the loop and "remember" state between iterations

    //a variable to remember the old date
    $olddate = '';
    //i named this variable $sql because it actually contains an sql query
    $sql = "SELECT date FROM staff WHERE date < '$Current'";
    //I named this variable $res because it actually contains NOT sql query, but mysql RESource.
    $res = mysql_query($sql) or treigger_error(mysql_error()." ".$sql);
    //I named this variable $row because it actually represents a row from the database.
    while($row=mysql_fetch_array($res)) {
      if ($olddate and $olddate != $row['date']) {
        echo "something here";
      }
      echo $row['date'];
      $olddate = $row['date'];
    }
    
    0 讨论(0)
  • 2021-01-16 08:02
    ("SELECT count(*), date FROM staff WHERE date < '$Current' GROUP BY date ORDER BY date asc"); 
    

    Group them up with a count, you can then write out each date the number of times indicated by the count, then write out your additional value.

    0 讨论(0)
  • 2021-01-16 08:08

    I think what you want can be achieved by sorting the results in the db and removing duplicates by grouping

    SELECT date FROM staff WHERE date < '$Current' ORDER BY date ASC GROUP BY date;
    
    0 讨论(0)
提交回复
热议问题