2015-12-02 70 views
0

所以我有一個MariaDB的數據庫中的表與幾行,如:與id參數排序行

+----+--------+-----------+---------+-----------+ 
| id | faseID | fase_tipo | fase_nr | tarefa_id | 
+----+--------+-----------+---------+-----------+ 
| 5 |  3 |   2 |  1 |   2 | 
| 6 |  3 |   2 |  2 |   2 | 
| 17 |  3 |   2 |  3 |   2 | 
| 12 |  3 |   3 |  1 |   6 | 
| 18 |  3 |   3 |  2 |   6 | 
+----+--------+-----------+---------+-----------+ 

通過生成:

SELECT id, 
     faseID, 
     fase_tipo, 
     fase_nr, 
     tarefa_id 
    FROM tarefas 
    WHERE obra = '15ID000' AND 
     faseID = '3' AND 
     tarefa_id <> '0' AND 
     tarefa_main = '2' 
    ORDER BY fase_tipo ASC 

我無法訂購此搜索結果,因爲我想有表排序爲:

+----+--------+-----------+---------+-----------+ 
| id | faseID | fase_tipo | fase_nr | tarefa_id | 
+----+--------+-----------+---------+-----------+ 
| 5 |  3 |   2 |  1 |   2 | 
| 6 |  3 |   2 |  2 |   2 | 
| 12 |  3 |   3 |  1 |   6 | 
| 18 |  3 |   3 |  2 |   6 | 
| 17 |  3 |   2 |  3 |   2 | 
+----+--------+-----------+---------+-----------+ 

我的意思是,使用字段tarefa_id到麥e這些行出現在該行的後面id。並在裏面訂購了fase_nr

是否有任何以tarefa_id爲目標的所有行在id = tarefa_id之後出現?

+2

你舉的例子是沒有意義的。沒有行'id' ='tarefa_id'。 –

+0

@gimley,我需要的是在'id'中出現'tarefa_id'的行在打印具有'id'的行後顯示。 @GordonLinoff,有一行'id' = 6和兩行'tarefa_id' = 6 – Comum

+0

幾個問題:1.爲什麼你的代碼說'ORDER BY fase_tipo',如果這不是你想要的? 2.爲什麼'id = 17'' id = 6'後面不出現? 3.是否有某個規則指出'id'總是大於'tarefa_id'? – Niklas

回答

1

看看下面的內容,它應該通過使用自加入和​​3210函數來給你想要的。

with test_data as (
     select 5 as id, 3 as faseID, 2 as fase_tipo, 1 as fase_nr, 2 as tarefa_id from dual 
     union all 
     select 6 as id, 3 as faseID, 2 as fase_tipo, 2 as fase_nr, 2 as tarefa_id from dual 
     union all 
     select 12 as id, 3 as faseID, 3 as fase_tipo, 1 as fase_nr, 6 as tarefa_id from dual 
     union all 
     select 18 as id, 3 as faseID, 3 as fase_tipo, 2 as fase_nr, 6 as tarefa_id from dual 
     union all 
     select 17 as id, 3 as faseID, 2 as fase_tipo, 3 as fase_nr, 2 as tarefa_id from dual 
    ) 
    -- This is the core you should pay attention to 
    select td1.* 
    from test_data td1 
    left join test_data td2 
    on td2.id = td1.tarefa_id 
    order by coalesce(td2.id, td1.id), td1.id, td1.fase_nr 
    -- 
; 

當然,我在Oracle中這樣做過,但總的想法應該適用。

輸出:

ID|FASEID|FASE_TIPO|FASE_NR|TAREFA_ID 
--+------+---------+-------+--------- 
5|  3|  2|  1|  2 
6|  3|  2|  2|  2 
12|  3|  3|  1|  6 
18|  3|  3|  2|  6 
17|  3|  2|  3|  2 

如果你的列是數字型的不是你需要將它們轉換order by子句中的排序:

with test_data as (
    select '12' as id, '3' as faseID, '3' as fase_tipo, '1' as fase_nr, '6' as tarefa_id from dual 
    union all 
    select '5' as id, '3' as faseID, '2' as fase_tipo, '1' as fase_nr, '2' as tarefa_id from dual 
    union all 
    select '18' as id, '3' as faseID, '3' as fase_tipo, '2' as fase_nr, '6' as tarefa_id from dual 
    union all 
    select '6' as id, '3' as faseID, '2' as fase_tipo, '2' as fase_nr, '2' as tarefa_id from dual 
    union all 
    select '17' as id, '3' as faseID, '2' as fase_tipo, '3' as fase_nr, '2' as tarefa_id from dual 
) 
-- This is the core you should pay attention to 
select td1.* 
from test_data td1 
left join test_data td2 
on td2.id = td1.tarefa_id 
order by to_number(coalesce(td2.id, td1.id)), to_number(td1.id), to_number(td1.fase_nr) 
-- 
; 
+0

我也在考慮左連接。這仍然給我一個錯誤的順序。 – Comum

+0

當我執行上述操作時,我會得到您在問題中指定的確切順序。 – gmiley

+0

我已將您的核心調整爲: 'select td1.id, td1.faseID,td1.fase_tipo,td1.fase_nr,td1.tarefa_id from tarefas td1 left tarefas td2 on td2.id = td1.tarefa_id where td1.obra ='15ID000'AND td1.faseID ='3'AND td1.tarefa_id <>'0'AND td1.tarefa_main ='2'by coalesce(td2.id,td1.id),td1.id,td1 .fase_nr; ' 而我得到了和第一張表一樣的東西。 – Comum