我正在运行SQL Server,我正在尝试从CRM应用程序加入多个表,但我无法得到我需要的结果。我发现这篇文章SQL Server: Multiple table joins with a WHERE clause包含了我正在寻找的信息,但我无法让它工作。
我有5张桌子
机器表
- MachID Name
- 1 Mach1
- 2 Mach2
- 3 Mach3
维护表
- MaintID StatusCode
- 1 1
- 2 1
维护扩展表
- MaintID MaintName StartDate EndDate MachId
- 1 Maint Test 1 2015-02-01 05:00:00.000 2015-02-03 05:00:00.000 2
- 2 Maint Test 2 2015-02-06 05:00:00.000 2015-02-09 05:00:00.000 2
日程表
- SchedID StatusCode
- 1 1
- 2 1
安排扩展表
- SchedID SchedName StartDate EndDate MachId JobNumber
- 1 Sched Test 1 2015-01-15 05:00:00.000 2015-01-19 05:00:00.000 2 1111
- 2 Sched Test 2 2015-01-20 05:00:00.000 2015-01-23 05:00:00.000 2 2222
所以我尝试这个查询,因为我想得到一组数据,当Maint事件中有一个NULL时,它会显示一个Schedule事件的行项目,同样在有一个时显示Maint事件并且为NULL在附表活动中。如果只有两个,那么只显示那个没有任何内容的项目。
DECLARE @StartDate DATETIME
DECLARE @EndDate DATETIME
SET @StartDate = '1/05/15'
SET @EndDate = '5/2/15'
SELECT
mn.name AS MachineName
,se.Schedname AS SchedName
,me.Maintname AS MaintName
,se.StartDate AS SchStartDate
,se.EndDate AS SchEndDate
,se.JobNumber
,sb.statuscode AS SchStat
,mb.statuscode AS MaintStat
,me.StartDate AS MaintStartDate
,me.EndDate AS MaintEndDate
FROM [machines] AS mn
INNER JOIN [MaintenanceExtension] AS me
ON mn.MachId = me.MachId
INNER JOIN [Maintenance] AS mb
ON me.MaintId = mb.MaintId
INNER JOIN [ScheduleExtension] as se
ON se.MachId = mn.MachId
INNER JOIN [Schedule] AS sb
ON sb.SchedId = se.SchedId
WHERE se.StartDate between @StartDate and @EndDate
我得到的,似乎包括带有Schedule项的MaintStartDate,反之亦然。为什么对立列不显示NULL?
MachineName SchedName MaintName SchStartDate SchEndDate JobNumber SchStat MaintStat MaintStartDate MaintEndDate
Mach2 SCH TEST 1 MAINT TEST 1 2015-01-15 05:00:00.000 2015-01-19 05:00:00.000 12345 1 1 2015-02-01 05:00:00.000 2015-02-03 05:00:00.000
Mach2 SCH TEST 2 MAINT TEST 1 2015-01-20 05:00:00.000 2015-01-23 05:00:00.000 7894 1 1 2015-02-01 05:00:00.000 2015-02-03 05:00:00.000
Mach2 SCH TEST 1 MAINT TEST 2 2015-01-15 05:00:00.000 2015-01-19 05:00:00.000 12345 1 1 2015-02-06 05:00:00.000 2015-02-09 05:00:00.000
Mach2 SCH TEST 2 MAINT TEST 2 2015-01-20 05:00:00.000 2015-01-23 05:00:00.000 7894 1 1 2015-02-06 05:00:00.000 2015-02-09 05:00:00.000
我正在寻找的是这样的:
MachineName SchedName MaintName SchStartDate SchEndDate JobNumber SchStat MaintStat MaintStartDate MaintEndDate
Mach2 SCH TEST 1 NULL 2015-01-15 05:00:00.000 2015-01-19 05:00:00.000 1111 1 1 NULL NULL
Mach2 SCH TEST 2 NULL 2015-01-20 05:00:00.000 2015-01-23 05:00:00.000 2222 1 1 NULL NULL
Mach2 NULL MAINT TEST 1 NULL NULL NULL 1 1 2015-02-01 05:00:00.000 2015-02-03 05:00:00.000
Mach2 NULL MAINT TEST 2 NULL NULL NULL 1 1 2015-02-06 05:00:00.000 2015-02-09 05:00:00.000
答案 0 :(得分:1)
INNER JOIN表示您想要连接两个表,其中列填充在两个表中。由于您需要具有MaintenanceExtension或ScheduleExtension的计算机的所有结果,因此请尝试使用此尺寸。
DECLARE @StartDate DATETIME
DECLARE @EndDate DATETIME
SET @StartDate = '1/05/15'
SET @EndDate = '5/2/15'
SELECT
mn.name AS MachineName
,se.Schedname AS SchedName
,me.Maintname AS MaintName
,se.StartDate AS SchStartDate
,se.EndDate AS SchEndDate
,se.JobNumber
,sb.statuscode AS SchStat
,mb.statuscode AS MaintStat
,me.StartDate AS MaintStartDate
,me.EndDate AS MaintEndDate
FROM [machines] AS mn
LEFT JOIN [MaintenanceExtension] AS me
ON mn.MachId = me.MachId
LEFT JOIN [Maintenance] AS mb
ON me.MaintId = mb.MaintId
LEFT JOIN [ScheduleExtension] as se
ON se.MachId = mn.MachId
LEFT JOIN [Schedule] AS sb
ON sb.SchedId = se.SchedId
WHERE se.StartDate between @StartDate and @EndDate
这将为您提供所有结果,包括既没有MaintenanceExtension也没有ScheduleExtension的计算机。如果要限制具有一个或另一个的计算机,则需要使用where where子句。
WHERE se.StartDate between @StartDate and @EndDate
AND (me.MachId IS NOT NULL OR se.MachID IS NOT NULL)
最后,如果要排除同时具有这两者的计算机,则需要使用where where子句。
WHERE se.StartDate between @StartDate and @EndDate
AND (me.MachId IS NOT NULL OR se.MachID IS NOT NULL)
AND (me.MachId IS NULL OR se.MACHId IS NULL)