SQL查詢的排序問題

2022-03-13 05:20:37 字數 778 閱讀 1924

1樓:

葉子部門是什麼意思?我理解的不知道對不對,

樓主所說的葉子部門應該是本身沒有子部門,並且有父部門的部門吧 ?如果沒有父部門就無從談起按父部門排序了。

在排序的條件使用子查詢即可。

select *

from 表 a

where fudepartment is not null and not exists(select * from 表 where fudepartment=a.id)

order by (select id from 表 where id=a.fudepartment),sunxu

這個查詢結果,會首先按父部門的部門id進行排序,之後再按各子部門在父部門中的順序排序。不知道是否符合樓主的需要。

另外如果,葉子部門可以有子部門,把not exists的條件去掉即可。

select *

from 表 a

where fudepartment is not null

order by (select id from 表 where id=a.fudepartment),sunxu

2樓:匿名使用者

select * from table1 order by fudepartment,sunxu

3樓:匿名使用者

你不可能通過一個sql查詢查出不出層次的葉子部門!

4樓:匿名使用者

用group by把父部門的分組然後再排序

sql查詢排序後前20條語句,SQL查詢排序後前20條語句

select c.d from select c,d,rownum rn from select t.from table1 order by b desc 降序 t1 where rn 20 注 用rownum的話,你要先排好序,然後再用rownum生成偽列,再限制行數。關於sql語句查詢排序 一...

sql查詢問題

select sum case when 欄位1 0 then 分數1 else 0 end sum case when 欄位2 0 then 分數2 else 0 end sum case when 欄位3 0 then 分數3 else 0 end from表 如果你的資料庫支援abs func...

SQL查詢資料語句問題,SQL查詢資料語句問題

取n到m行 1.select top m from tablename where id not in select top n id from tablename order by id asc desc 2.select top m into 臨時表 或表變數 from tablename or...