我理解你的問題是每乙個使用者id在乙個部門中所有的許可權,你給的資料好像不詳細,我新增了一些資料,你看看滿足需求不。sql> with authority as (
2 select '3029' kuid,'205' krid,'21' kdid from dual union all
3 select '3029' kuid,'206' krid,'21' kdid from dual union all
4 select '3029' kuid,'209' krid,'21' kdid from dual union all
5 select '3030' kuid,'205' krid,'10080' kdid from dual union all
6 select '3030' kuid,'206' krid,'10080' kdid from dual union all
7 select '3030' kuid,'209' krid,'10080' kdid from dual union all
8 select '3029' kuid,'205' krid,'10080' kdid from dual union all
9 select '3029' kuid,'206' krid,'10080' kdid from dual union all
10 select '3029' kuid,'209' krid,'10080' kdid from dual
11 ),
12 dept as (
13 select '0' kdid,'-1' kdpid from dual union all
14 select '10080' kdid,'0' kdpid from dual union all
15 select '21' kdid,'10080' kdpid from dual union all
16 select '1' kdid,'-1' kdpid from dual union all
17 select '20080' kdid,'1' kdpid from dual union all
18 select '31' kdid,'20080' kdpid from dual
19 )
20 select distinct t1.kuid,
21 t1.krid,
22 t2.kdid kdpid
23 from authority t1
24 inner join (select t.*,
25 connect_by_root(t.kdid) root_id
26 from dept t
27 start with t.kdid is not null
28 connect by prior t.kdpid = t.kdid) t2 on t1.kdid = t2.root_id
29 order by kuid,
30 kdpid,
31 krid
32 ;
kuid krid kdpid
3029 205 0
3029 206 0
3029 209 0
3029 205 10080
3029 206 10080
3029 209 10080
3029 205 21
3029 206 21
3029 209 21
3030 205 0
3030 206 0
3030 209 0
3030 205 10080
3030 206 10080
3030 209 10080
15 rows selected
一條SQL語句研究
現有 select from t where a in 5,3,2,1,8,9,30.假設 a 是主鍵,in裡面的引數是唯一的。現要求輸出的結果集按照 in 提供的引數順序排序。而不是按照a本身的排序規則排序?另 如果不要求使用臨時表或表變數,那麼又有什麼辦法實現。臨時表方案參卡 create ta...
一條SQL語句求每月連續低溫時間
近期為某個專案寫儲存過程做統計。其中有個是這樣的 求每個月至少連續3個小時低於某個溫度值的時間 小時 假設有個全年溫度表 sql view plain copy create table t m int,h int,t decimal 18,4 m 月份 h 小時 t 溫度值 一年有8760小時,因...
一條sql 語句的優化
第二個版本 一條sql搞定,使用巢狀查詢,費時2 3分鐘 select a.indexid,c.title,c.createdtime,c.intro,d.picurl,e.src,e.size,e.info from mms content index a,mms index node b,mms...