一个以上的mysql命令语句

时间:2015-10-13 05:35:50

标签: mysql sql-order-by

我有一张桌子,我需要按照性别,部门和年级使用排序方式对要排列的值进行排序,是否有人知道如何执行此操作?

这是我的sql语句:

select
   * 
from
   (select
      * 
   from
     (SELECT
        distinct a.LRN,
        a.middle_initial,
        a.last_name,
        a.first_name,
        a.gender,
        a.grade_level,
        c.section_name,
        b.school_year,
        d.grade_levelnumeric 
     from
        tblstudent as a 
     inner join
        tblclass as b 
           on b.LRN = a.LRN 
     inner join
        tblsection as c 
           on c.section_id=b.section_id 
     inner join
        tblgradelevel as d 
           on b.grade_level=d.grade_level 
     where
        b.school_year = '" & Comboschoolyear.Text & "' 
     order by
        d.grade_levelnumeric DESC) as temptable 
  order by
     section_name) as temptable1 
  order by
     gender;

我的问题是我无法正确输出。谁能帮我?非常感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

您的最终输出结果不正确,因为在最外层查询中您使用了order by gender,因此它将按性别排列,它可能维护内部子查询的顺序,也可能不维护内部子查询的顺序。 你可以通过 -

来做到这一点
SELECT
        distinct a.LRN,
        a.middle_initial,
        a.last_name,
        a.first_name,
        a.gender,
        a.grade_level,
        c.section_name,
        b.school_year,
        d.grade_levelnumeric 
     from
        tblstudent as a 
     inner join
        tblclass as b 
           on b.LRN = a.LRN 
     inner join
        tblsection as c 
           on c.section_id=b.section_id 
     inner join
        tblgradelevel as d 
           on b.grade_level=d.grade_level 
     where
        b.school_year = '" & Comboschoolyear.Text & "' 
     order by
        gender ASC, section_name ASC, d.grade_levelnumeric DESC

这将按第一个性别排列记录,然后是section_name然后是等级。您可以根据需要交换年级,部分和性别。