Php 按多列对sql结果排序时出现的问题

Php 按多列对sql结果排序时出现的问题,php,mysql,sql,distinct,Php,Mysql,Sql,Distinct,我需要显示“年”和“月号”列的所有独特组合。我在排序结果时遇到问题,因此无法首先显示最新的组合(2014年9月)。结果应该是: year monthnumber sales ------------------------ 2014 8 2800 2014 9 3000 2013 11 2120 2014 8 2700 2013 7 2600 2013 12

我需要显示“年”和“月号”列的所有独特组合。我在排序结果时遇到问题,因此无法首先显示最新的组合(2014年9月)。结果应该是:

year  monthnumber  sales
------------------------
2014       8       2800
2014       9       3000
2013      11       2120
2014       8       2700
2013       7       2600
2013      12       2100
到目前为止,下面的代码给出了以下结果(因此在订购月份时出现了一些问题):

Php/Sql:

september 2014
august 2014
july 2013
december 2013
november 2013

我怀疑您需要在
ORDER BY
子句中将
MonthNumber
转换为
整数

<?php
 $sql = "SELECT DISTINCT (case  when monthnumber = '1' then 'january'
                                when monthnumber = '2' then 'february'
                                when monthnumber = '3' then 'march'
                                when monthnumber = '4' then 'april'
                                when monthnumber = '5' then 'may'
                                when monthnumber = '6' then 'june'
                                when monthnumber = '7' then 'july'
                                when monthnumber = '8' then 'august'
                                when monthnumber = '9' then 'september'
                                when monthnumber = '10' then 'october'
                                when monthnumber = '11' then 'november'
                                when monthnumber = '12' then 'december'
                         end) AS monthname, year from `exampletable` ORDER BY year DESC, monthnumber DESC";

 $stmt = $pdo->prepare($sql);
 $stmt->execute();

 if($stmt->rowCount())
 {
 while ($result = $stmt->fetch(PDO::FETCH_ASSOC))
 {
?>

<?php echo strtolower($result['monthname']);?> <?php echo $result['year'];?>

<?php
 }// end while
 }// end if
 else {
 echo '0 results';
 }// end else
?>

如果MontNumber是一个字符串,则排序顺序首先对第一个字符进行排序,然后对第二个字符进行排序;按下降顺序排序时,应给出如下内容:

九, 8. 7. 6. 5. 4. 3. 2. 12 11 10 一,


正如另一个答案中所述,您必须首先将MonthNumber从字符串转换为整数。

您需要按顺序进行简单类型转换
<?php
 $sql = "SELECT DISTINCT (case  when monthnumber = '1' then 'january'
                                when monthnumber = '2' then 'february'
                                when monthnumber = '3' then 'march'
                                when monthnumber = '4' then 'april'
                                when monthnumber = '5' then 'may'
                                when monthnumber = '6' then 'june'
                                when monthnumber = '7' then 'july'
                                when monthnumber = '8' then 'august'
                                when monthnumber = '9' then 'september'
                                when monthnumber = '10' then 'october'
                                when monthnumber = '11' then 'november'
                                when monthnumber = '12' then 'december'
                         end) AS monthname, year from `exampletable` ORDER BY year DESC, monthnumber DESC";

 $stmt = $pdo->prepare($sql);
 $stmt->execute();

 if($stmt->rowCount())
 {
 while ($result = $stmt->fetch(PDO::FETCH_ASSOC))
 {
?>

<?php echo strtolower($result['monthname']);?> <?php echo $result['year'];?>

<?php
 }// end while
 }// end if
 else {
 echo '0 results';
 }// end else
?>
ORDER BY year DESC, CAST(monthnumber AS UNSIGNED) DESC