如何在MySQL的左外连接中有序显示类别

时间:2013-01-13 12:51:55

标签: php mysql sql sql-order-by

如何按qid排序显示此查询?

  $newcount=$db->get_results("
        SELECT  s2.qcategory, 
                s1.id, 
                count(s1.na) as na_count 
        FROM    (
                    select  distinct `qcategory` 
                    from    store 
                    where   survey_name='$userID' and 
                            dateone='$dateVal' and 
                            branch='$branch'
                ) s2 
                left join store s1 
                    on s1.`qcategory` = s2.`qcategory` and 
                        s1.`na` = '1' 
        group by 1 
        order by s1.qid");

2 个答案:

答案 0 :(得分:1)

你可能想订购ASC或DESC

试试这个

     ORDER BY s1.qid  ASC   

   ORDER BY s1.qid  DESC

以及为什么要制作GROUP BY 1 ??

答案 1 :(得分:0)

由于数据类型为VARCHAR,您需要将其转换为数字,例如

ORDER BY CAST(s1.qid AS SIGNED) ASC