0

我的数据库有以下表结构:

  • tbl_project

  • tbl_employee

  • tbl_deliverable
  • user_to_deliverable

其中tbl_prjcttbl_deliverable具有一对多关系, 而tbl_employeetbl_deliverable具有多对多关系,因此它们被拆分为user_to_deliverable表。

我想要一个查询来显示 project_name(来自 tbl_project)、项目的可交付成果(来自 tbl_deliverable)以及该特定交付成果分配给的员工姓名。

我可以获得编写此查询的帮助吗?

4

2 回答 2

1

您想要的查询很像这样的 89.67%:D

SELECT      a.ProjectName,
            b.deliverables,
            d.employeename
FROM        tbl_project a
                INNER JOIN tbl_deliverable b
                    ON a.projectID = b.projectID
                INNER JOIN user_to_deliverable c
                    ON b.recordID = c.RecordID      
                        -- or could be the primary key
                INNER JOIN tbl_employee d
                    ON c.userID = d.userID
于 2012-09-04T00:31:46.853 回答
0

不知道您的架构的近似解决方案。如果您希望将每个项目的可交付成果/员工连接起来,最好通过检索结果并以非 SQL 语言(PHP、Python 或您正在使用的任何语言)进行解析来完成。如果您希望这完全在 SQL 中完成,您将需要使用GROUP_CONCAT().

select tbl_project.name as project,
       tbl_deliverable.name as deliverable,
       tbl_employee.name as employee
from tbl_project
join tbl_deliverable
    on (tbl_project.id = tbl_deliverable.project_id)
join user_to_deliverable
    on (tbl_deliverable.id = user_to_deliverable.deliverable_id)
join tbl_employee
    on (user_to_deliverable.employee_id = tbl_employee.id)
于 2012-09-04T00:44:18.613 回答