0

我的排序有问题,我不明白问题出在哪里。我有一张桌子

id  id_teacher     subject  class   hour           day
1   2 [->]           Math      X C     8         Monday
2   2 [->]           Math      X C     12       Wednesday
3   2 [->]           Math      X C     9         Tuesday
4   2 [->]           Math      VI B    10       Monday
5   2 [->]           Math      X C     11       Monday
6   2 [->]           Math      X C     10       Tuesday
7   5 [->]           Chimie   X C     9         Monday
8   5 [->]           Chimie   X C     12       Monday
9   2 [->]           Sport     X C      7         Monday

而且我有一个打印我“小时”和“主题”的功能。功能是这样的:

function OreMonday($item){
        $sth = $this->dbh->prepare("SELECT class FROM elevi WHERE id_elev = :id_elev;");
        $sth->bindParam(":id_elev", $item);   
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_ASSOC);
        $sth1 = $this->dbh->prepare("SELECT hour, subject, day FROM hourr WHERE class = :class ORDER BY hour DESC;");
        $sth1->bindParam(":class", $result['class']);   
        $sth1->execute();
        while ($result1 = $sth1->fetch(PDO::FETCH_ASSOC)) {
            if ($result1['day'] == 'Monday') {
                echo $result1['hour'];
                echo $result1['subject']."<br>";
            }
        }    
    }

}

$item 是 $_SESSION['id'] 并且 $result['class'] 是另一个语句的 XC

结果是这样的:

Monday
9Chimie
8Math
7Sport
12Chimie
11Math

而且我要:

Monday
7Sport
8Math
9Chimie
11Math
12Chimie
4

1 回答 1

4
  1. 在数据库中,小时列应该是数字列,而不是 varchar 或字符串列*
  2. 您应该订购 ASC,而不是 DESC

*如果(且仅当)由于某种原因您无法更改列类型:检查mysql 排序字符串编号

于 2013-10-25T11:50:17.687 回答