组合多个SQL JOIN

时间:2010-08-26 15:36:44

标签: sql inner-join

ALTER PROCEDURE [dbo].[GetValues]
@FieldName NVARCHAR(50),
@FormName NVARCHAR(50),
@PoolName NVARCHAR(50)

AS SELECT FieldValue FROM [dbo].[Values]

INNER JOIN [dbo].[Fields]
        ON [dbo].[Fields].FieldID = [dbo].[Values].FieldID

INNER JOIN [dbo].[FormFields]
        ON [dbo].[FormFields].FieldID = [dbo].[Fields].FieldID

INNER JOIN [dbo].[Forms]
        ON [dbo].[Forms].FormID = [dbo].[FormFields].FormID

INNER JOIN [dbo].[Pools]
        ON [dbo].[Pools].FormID = [dbo].[Forms].FormID

WHERE [dbo].[Fields].FieldName = @FieldName
  AND [dbo].[Forms].FormName = @FormName
  AND [dbo].[Pools].PoolName = @PoolName

我希望此代码按字段,表单和池名称过滤值。但它只按字段名称过滤。怎么了?

表单

FormID    FormName
96        FormA
98        FormB
97        FormC

PoolID    FormID    PoolName
29        96        PoolA1
31        98        PoolB1
30        97        PoolC1

记录

RecordID    PoolID
42          29
43          29
44          29
45          31
46          31
47          31

FieldID    RecordID    FieldValue
101        42          Yellow
101        43          Yellow
101        44          Yellow
101        45          Pink
101        46          Pink
101        47          Pink
102        42          Smith
102        43          Jones
102        44          Fletchers
103        42          Fred
103        43          Bob
103        44          Marty

例如,如果我使用“收藏颜色”(FieldID = 101),“FormA”(FormID = 96)和“PoolA1”(PoolID = 29)进行过滤,则会显示“黄色,黄色,黄色,粉红色,粉红色” ,粉红色“,但它应该是”黄色,黄色,黄色“。

3 个答案:

答案 0 :(得分:1)

首先,我建议颠倒你的WHERE语句的顺序,查询应该执行得更快。在SSMS中运行查询并打开查询计划,您将清楚地看到这一点。

其次,我认为它应该有效,你是否使用该查询完成了SELECT * FROM,以确保它不是坏数据的问题。

答案 1 :(得分:1)

您发布的代码没有任何问题。所以它必须是别的东西。 (例如电视遥控器很好,但没有插入电视)

1)检查proc中的代码是否与您认为的一样

sp_helptext 'GetValues'

2)确保只有一个GetValues而您没有使用其他用户的GetValues,例如asmo.GetValues

select * from information_schema.routines where specific_name=  'GetValues'

3)如果失败,则从proc内部获取SQL。硬编码参数值并运行它,看它是否返回您认为应该的结果。如果它没有将该sql发布到SO。

编辑根据您添加的表我尝试复制您遇到的问题,但SQL中的表名和数据名不一样,并且在数。所以我试着合成这两个。以下工作。我希望它有所帮助

Create Database Test
GO

USE Test
GO

CREATE Table Forms
(FormID int,
FormName nvarchar(50))
GO

Create Table Pools
(PoolID int,
FormID int,
PoolName nvarchar(50))
GO

Create Table Records
(RecordID int,
PoolID int)
GO

Create Table [Values]
(FieldID int,
RecordID int,
FieldValue nvarchar(50))
GO

CREATE Table [Fields]
(
FieldID int,
FieldName nvarchar(100)
)
GO

CREATE Table [FormFields]
(
FieldID int,
FormID int
)
GO

INSERT INTO Forms (FormID, FormName) VALUES (96, 'FormA')
INSERT INTO Forms (FormID, FormName) VALUES (98, 'FormB')
INSERT INTO Forms (FormID, FormName) VALUES (97, 'FormC')
GO

INSERT INTO Pools (PoolID, FormID, PoolName) Values (29, 96, 'PoolA1')
INSERT INTO Pools (PoolID, FormID, PoolName) Values (31, 98, 'PoolB1')
INSERT INTO Pools (PoolID, FormID, PoolName) Values (30, 97, 'PoolC1')
GO

INSERT INTO Records (RecordID, PoolID) Values (42,  29)
INSERT INTO Records (RecordID, PoolID) Values (43,  29)
INSERT INTO Records (RecordID, PoolID) Values (44,  29)
INSERT INTO Records (RecordID, PoolID) Values (45,  31)
INSERT INTO Records (RecordID, PoolID) Values (46,  31)
INSERT INTO Records (RecordID, PoolID) Values (47,  31)

GO

INSERT INTO [Values] (FieldID, RecordID, FieldValue) Values (101, 42, 'Yellow')
INSERT INTO [Values] (FieldID, RecordID, FieldValue) Values (101, 43, 'Yellow')
INSERT INTO [Values] (FieldID, RecordID, FieldValue) Values (101, 44, 'Yellow')
INSERT INTO [Values] (FieldID, RecordID, FieldValue) Values (101, 45, 'Pink')
INSERT INTO [Values] (FieldID, RecordID, FieldValue) Values (101, 46, 'Pink')
INSERT INTO [Values] (FieldID, RecordID, FieldValue) Values (101, 47, 'Pink')
INSERT INTO [Values] (FieldID, RecordID, FieldValue) Values (102, 42, 'Smith')
INSERT INTO [Values] (FieldID, RecordID, FieldValue) Values (102, 43, 'Jones')
INSERT INTO [Values] (FieldID, RecordID, FieldValue) Values (102, 44, 'Fletchers')
INSERT INTO [Values] (FieldID, RecordID, FieldValue) Values (103, 44, 'Fred')
INSERT INTO [Values] (FieldID, RecordID, FieldValue) Values (103, 44, 'Bob')
INSERT INTO [Values] (FieldID, RecordID, FieldValue) Values (103, 44, 'Marty')

GO

INSERT INTO [Fields] (FieldID, FieldName) Values (101, 'Color')
INSERT INTO [Fields] (FieldID, FieldName) Values (102, 'Last Name')
INSERT INTO [Fields] (FieldID, FieldName) Values (103, 'First Name')

GO

INSERT INTO FormFields (FieldID, FormID) Values (101, 96)
INSERT INTO FormFields (FieldID, FormID) Values (102, 96)
INSERT INTO FormFields (FieldID, FormID) Values (103, 96)
INSERT INTO FormFields (FieldID, FormID) Values (101, 97)
INSERT INTO FormFields (FieldID, FormID) Values (102, 97)
INSERT INTO FormFields (FieldID, FormID) Values (103, 97)
INSERT INTO FormFields (FieldID, FormID) Values (101, 98)
INSERT INTO FormFields (FieldID, FormID) Values (102, 98)
INSERT INTO FormFields (FieldID, FormID) Values (103, 98)
GO





Create PROCEDURE [dbo].[GetValues]
@FieldName NVARCHAR(50),
@FormName NVARCHAR(50),
@PoolName NVARCHAR(50)

AS 

SELECT
    v.FieldValue 
FROM
    dbo.Forms f 
    INNER JOIN dbo.Pools p ON f.FormID = p.FormID
    INNER JOIN dbo.FormFields ff on f.FormID = ff.FormID
    INNER JOIN dbo.Fields fd on fd.FieldID = ff.FieldID
    INNER JOIN dbo.Records r on p.PoolID = r.PoolID 
    INNER JOIN dbo.[Values] v on r.RecordID  = v.RecordID 
        and ff.FieldID = v.FieldID


WHERE 
  fd.FieldName = @FieldName
  and f.FormName = @FormName
  AND p.PoolName = @PoolName
 GO



  dbo.getValues 'Color', 'FormA', 'PoolA1'

答案 2 :(得分:1)

正如我所想,问题出在存储过程本身。其他一切都很好。 JOIN错误地完成了,这就是我得到错误结果的原因。这是解决方案:

ALTER PROCEDURE [dbo].[GetValues]
@FieldName NVARCHAR(50),
@FormName NVARCHAR(50),
@PoolName NVARCHAR(50)

 AS SELECT FieldValue
      FROM [dbo].[Values]

INNER JOIN [dbo].[Fields]
        ON [dbo].[Fields].FieldID = [dbo].[Values].FieldID

INNER JOIN [dbo].[Records]
        ON [dbo].[Records].RecordID = [dbo].[Values].RecordID

INNER JOIN [dbo].[Pools]
        ON [dbo].[Pools].PoolID = [dbo].[Records].PoolID

INNER JOIN [dbo].[Forms]
        ON [dbo].[Forms].FormID = [dbo].[Pools].FormID

     WHERE [dbo].[Fields].FieldName = @FieldName
       AND [dbo].[Forms].FormName = @FormName
       AND [dbo].[Pools].PoolName = @PoolName