我有两张桌子:
第一表数据(工作流程):
workflow_id, name, process_id, branch_id, is_parralel , flow_order
80, AA, 27, 0, No, 1
81, , 27, 0, Yes, 2
82, CC, 27, 0, No, 3
83, BB, 27, 116, No, 1
84, DD, 27, 117, No, 1
85, EE, 27, 116, No, 2
第二表数据(workflow_parallel_branch):
id, workflow_id, branch_name
116, 81, Branch1
117, 81, Branch2
我想以下面的格式获取数据。我该如何进行此查询?
workflow_id, name
80, AA
83, BB
85, EE
84, DD
82, CC
答案 0 :(得分:0)
我猜他希望他们按照80,81,82的顺序,其中81被指定的分支116,116,117替换。然后回到原始顺序 - > 82
不知道这在mysql中是否有效:
with workflow (workflow_id, name, branch_id) as
(values (80, 'AA', 0), (81, NULL, 0), (82, 'CC', 0),
(83, 'BB', 116), (84, 'DD', 117), (85, 'EE', 116)),
workflow_parallel_branch (id, workflow_id) as
(values (116, 81), (117, 81)),
tmp (id, br, orig, name) as
(select coalesce(b.workflow_id, a.workflow_id), a.branch_id,
a.workflow_id, a.name
from workflow a
left join workflow_parallel_branch b on a.branch_id = b.id
order by a.workflow_id, a.branch_id)
select orig, name
from tmp
where name is not null
order by id,br
==>
ORIG NAME
80 AA
83 BB
85 EE
84 DD
82 CC