2015-12-02 71 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