我需要帮助将两行与以下要求结合起来(使用表格更容易演示..):
我的架构如下所示:
项目表:
project_id | name
--------------------------------
1 Project1
2 Project2
3 Project3
project_program 表
id | project_id | program_id
-----------------------------------
1 1 1
2 1 2
2 3 2
节目表:
program_id | type | name
---------------------------------------
1 A test
2 B production
选择语句所需的输出:
project_id | name | A | B
-------------------------------------------------------------------
1 Project1 test production
2 Project2 (null) (null)
2 Project3 (null) production
这是我迄今为止设法实现的目标:
SELECT
project.*,
CASE WHEN program.type LIKE 'A' THEN program.name ELSE NULL END AS A,
CASE WHEN program.type LIKE 'B' THEN program.name ELSE NULL END AS B
FROM
project
LEFT JOIN project_program ON project.project_id = project_program.project_id
LEFT JOIN program ON project_program.program_id = program.program_id
它很接近但不完全。现在我的结果如下所示:
project_id | name | A | B
-------------------------------------------------------------------
1 Project1 test (null)
1 Project1 (null) production
2 Project2 (null) (null)
3 Project3 (null) production
我正在使用 Oracle 11g。