我有3个不同的表:
process table:
| projectNo | process | studio |
+-----------+---------+--------+
| 170001 | ANM BLD | APEX |
| 170001 | ANM BLD | CANVAS |
| 170002 | CGI MOD | APEX |
| 170003 | CGI MOD | ORBIT |
proc_leader table:
| projectNo | process | proc_leader|
+-----------+---------+------------+
| 170001 | ANM BLD | John |
| 170001 | ANM BLD | James |
| 170002 | CGI MOD | Peter |
| 170003 | CGI MOD | Kate |
proc_checker table:
| projectNo | process | proc_checker|
+-----------+---------+------------+
| 170001 | ANM BLD | Aaron |
| 170001 | ANM BLD | John |
| 170002 | CGI MOD | Peter |
| 170003 | CGI MOD | Adel |
所以我想做的是创建html表,如下所示:
| username | studio APEX | studio CANVAS | studio ORBIT |
+----------+-------------+---------------+--------------+
| Aaron | x | x | |
| Adel | | | x |
| John | x | x | |
| James | x | x | |
| Kate | | | x |
| Peter | x | | |
所以我想将流程表中的工作室分配给用户名。有可能吗?
答案 0 :(得分:1)
您可以UNION
使用LEFT JOIN
表process
领导和检查表。
试试这个:
select
p2.proc_leader,
case count(case when p1.studio = 'APEX' then 1 end) > 0 then 'x' end,
case count(case when p1.studio = 'CANVAS' then 1 end) > 0 then 'x' end,
case count(case when p1.studio = 'ORBIT' then 1 end) > 0 then 'x' end
from process p1
left join (
select *
from proc_leader
union all
select *
from proc_checker
) p2 on p1.projectNo = p2.projectNo
and p1.process = p2.process
group by p2.proc_leader;
答案 1 :(得分:1)
我认为以下是您想要的:
select proc_leader,
max(case when studio = 'APEX' then 'x' else '' end) as APEX,
max(case when studio = 'CANVAS' then 'x' else '' end) as CANVAS,
max(case when studio = 'ORBIT' then 'x' else '' end) as ORBIT
from ((select l.proc_leader, p.studio
from proc_leader l join
process p
on l.projectNo = p.projectNo
) union all
(select c.proc_leader, p.studio
from proc_checker c
process p
on c.projectNo = p.project_no
)
) pp
group by proc_leader;
答案 2 :(得分:1)
SELECT
u.username,
max(if (p.studio = 'APEX', 'x', '')) AS APEX,
max(if (p.studio = 'CANVAS', 'x', '')) AS CANVAS,
max(if (p.studio = 'ORBIT', 'x', '')) AS ORBIT
FROM (
SELECT proc_leader AS username,projectNo FROM proc_leader
UNION
SELECT proc_checker AS username,projectNo FROM proc_checker
) AS u
LEFT JOIN process AS p ON p.projectNo = u.projectNo
GROUP BY 1