0

好的,在这个查询中,我从 5 个表中提取信息,表 Company、Programmer、Tester、Manager 和表 Contract。我将提取程序员、测试人员和经理的姓名和电话号码,以及他们工作的公司,这家公司负责管理这个程序,因为 x 人的请求无关紧要。

问题在于下面的代码,某个信息会出现与其他信息一样多的信息,例如程序员的姓名和电话号码将与公司的经理和测试人员一样多地出现。

我尝试了左外连接,它会给我更多的结果,那么我该如何解决这个问题,以便下次结果不会重复但说 NULL?

SELECT DISTINCT pg.name,
                pg.Tel_Nr,
                Mgr.name,
                Mgr.Tel_Nr,
                Ts.Name,
                Ts.Tel_Nr,
                Pg.Name,
                con.program_name
FROM Company AS Cm
INNER JOIN Programmer AS Pg ON Pg.company = Cm.name
INNER JOIN Manager AS Mg ON Mg.company = Cm.name
INNER JOIN Tester AS Ts ON Ts.company = Cm.name
INNER JOIN Contract AS Con ON Con.program_name = 'My Program'
AND Cm.name = Con.Company
4

1 回答 1

0

当然,生成一份联系方式列表以及可能的职位描述会更有意义。像这样的东西:

WITH Cte as (select Cm.name from 
      Contract as Con join Company as Cm on  Cm.name = Con.Company
     where Con.program_name = 'My Program')
SELECT pg.name,  pg.Tel_Nr, 'Programmer' as JobTitle
FROM  Cte   INNER JOIN
    Programmer as Pg  on Pg.company = Cte.name 
UNION ALL
SELECT Mgr.name,  Mgr.Tel_Nr,'Manager' as JobTitle
FROM  Cte   INNER JOIN
     Manager as Mg on Mg.company= Cte.name 
UNION ALL
SELECT Ts.Name,  Ts.Tel_Nr, 'Tester' as JobTitle
FROM  Cte   INNER JOIN
    Tester as Ts on Ts.company = Cte.name 

该解决方案部署了一个公用表表达式(标记为Cte)以避免多次 查询 thrCompany和表。了解更多Contract

于 2013-01-06T17:45:54.050 回答