我目前有以下5个表格。每个站点都有(1个或多个)项目经理以及(1个或多个)Site_Supervisors。项目经理/现场主管可以分配到许多站点
我目前有一个有效的查询:
SELECT Sites.Site_Name, Sites.Site_Street_Address, Sites.Site_Suburb, Sites.Site_State, Sites.Site_Postcode, Sites.Site_Region, Sites.Completed, Project_Managers.First_Name AS PM_First_Name, Project_Managers.Last_Name AS PM_Last_Name, Site_Supervisors.First_Name AS SS_First_Name, Site_Supervisors.Last_Name AS SS_Last_Name, Sites.Date_Started
FROM Site_Site_Supervisors INNER JOIN
Site_Supervisors ON Site_Site_Supervisors.Site_Supervisor_ID = Site_Supervisors.Site_Supervisor_ID RIGHT OUTER JOIN
Sites ON Site_Site_Supervisors.Site_ID = Sites.Site_ID LEFT OUTER JOIN
Project_Managers INNER JOIN
Site_Project_Managers ON Project_Managers.Project_Manager_ID = Site_Project_Managers.Project_Manager_ID ON
Sites.Site_ID = Site_Project_Managers.Site_ID
WHERE (Sites.Completed = 0)
ORDER BY Sites.Site_Name
这给了我想要的输出,然后我在我的代码中使用它。
我想要对查询进行更改,而不是输出:
Project_Managers.First_Name AS PM_First_Name
Project_Managers.Last_Name AS PM_Last_Name
而是用以下内容替换它们:
Project_Managers.First_Name其中对应的Site_Project_Managers.Primary_Contact = True AS PM_First_Name
Project_Managers.Last_Name其中对应的Site_Project_Managers.Primary_Contact = True AS PM_Last_Name
Project_Managers.First_Name其中对应的Site_Project_Managers.Primary_Contact = False AS AssistantPM_First_Name
Project_Managers.Last_Name其中对应的Site_Project_Managers.Primary_Contact = False AS AssistantPM_Last_Name
此时执行此查询超出了我的SQL技能组,所以我希望你们其中一个人能够提供正确的查询以供使用或者某些指导。 感谢
[Site_ID] [int] IDENTITY(1,1) NOT NULL (PK),
[Site_Name] [varchar](50) NOT NULL,
[Site_Street_Address] [varchar](50) NULL,
[Site_Suburb] [varchar](50) NULL,
[Site_State] [varchar](50) NULL,
[Site_Postcode] [varchar](10) NULL,
[Site_Region] [varchar](50) NULL,
[Date_Started] [datetime] NULL,
[Completed] [bit] NOT NULL
[Project_Manager_ID] [int] IDENTITY(1,1) NOT NULL (PK),
[First_Name] [varchar](50) NOT NULL,
[Last_Name] [varchar](50) NOT NULL,
[Phone_Number] [varchar](50) NULL,
[Email_Address] [varchar](50) NULL,
[Currently_Employed] [bit] NOT NULL,
CONSTRAINT [PK_Project_Managers] PRIMARY KEY CLUSTERED
[Site_Supervisor_ID] [int] IDENTITY(1,1) NOT NULL (PK),
[First_Name] [varchar](50) NOT NULL,
[Last_Name] [varchar](50) NOT NULL,
[Phone_Number] [varchar](50) NULL,
[Email_Address] [varchar](50) NULL,
[Currently_Employed] [bit] NOT NULL,
CONSTRAINT [PK_Site_Supervisors_1] PRIMARY KEY CLUSTERED
[Site_ID] [int] NOT NULL (PK),
[Project_Manager_ID] [int] NOT NULL (PK),
[Primary_Contact] [bit] NULL,
[Alerts] [bit] NULL,
CONSTRAINT [PK_Site_Project_Managers] PRIMARY KEY CLUSTERED
[Site_ID] [int] NOT NULL (PK),
[Site_Supervisor_ID] [int] NOT NULL (PK),
[Primary_Contact] [bit] NULL,
[Alerts] [bit] NULL,
CONSTRAINT [PK_Site_Site_Supervisors] PRIMARY KEY CLUSTERED
答案 0 :(得分:2)
假设您正在使用MySql并且您可以使用CASE或内联IF ELSE来获得所需的输出
SELECT Sites.Site_Name,
Sites.Site_Street_Address,
Sites.Site_Suburb,
Sites.Site_State,
Sites.Site_Postcode,
Sites.Site_Region,
Sites.Completed,
CASE Site_Project_Managers.Primary_Contact
WHEN true THEN Project_Managers.First_Name
END AS PM_First_Name,
CASE Site_Project_Managers.Primary_Contact
WHEN true THEN Project_Managers.Last_Name
END AS PM_Last_Name,
CASE Site_Project_Managers.Primary_Contact
WHEN false THEN Project_Managers.First_Name
END AS AssistantPM_First_Name,
CASE Site_Project_Managers.Primary_Contact
WHEN false THEN Project_Managers.Last_Name
END AS AssistantPM_Last_Name,
Site_Supervisors.First_Name AS SS_First_Name,
Site_Supervisors.Last_Name AS SS_Last_Name,
Sites.Date_Started
FROM Site_Site_Supervisors
INNER JOIN Site_Supervisors
ON Site_Site_Supervisors.Site_Supervisor_ID = Site_Supervisors.Site_Supervisor_ID
RIGHT OUTER JOIN Sites
ON Site_Site_Supervisors.Site_ID = Sites.Site_ID
LEFT OUTER JOIN Project_Managers
INNER JOIN Site_Project_Managers
ON Project_Managers.Project_Manager_ID = Site_Project_Managers.Project_Manager_ID
ON Sites.Site_ID = Site_Project_Managers.Site_ID
WHERE (Sites.Completed = 0)
ORDER BY Sites.Site_Name