1

我有一个数据库和两个表,Male两个Femaleint primary key with the name ID都有一个外键,两个表的外键都在一个单独的名为RecommendFemale.

男表:

男性 :

[dbo].[Male] (
    [ID]            INT           IDENTITY (1, 1) NOT NULL,
    [Image]         VARCHAR (MAX) NULL,
    [Name]          VARCHAR (50)  NULL,
    [Age]           INT           NULL,
    CONSTRAINT [PK_Female] PRIMARY KEY CLUSTERED ([ID] ASC)
);

女性 :

 [dbo].[Female] (
        [ID]            INT           IDENTITY (1, 1) NOT NULL,
        [Image]         VARCHAR (MAX) NULL,
        [Name]          VARCHAR (50)  NULL,
        [Age]           INT           NULL,
        CONSTRAINT [PK_Female] PRIMARY KEY CLUSTERED ([ID] ASC)
    );

推荐 :

[dbo].[RecommendFemale] (
    [ID]          INT IDENTITY (1, 1) NOT NULL,
    [RecommendTo] INT NOT NULL,
    [Recommend]   INT NOT NULL,
    CONSTRAINT [PK_RecommendFemale] PRIMARY KEY CLUSTERED ([ID] ASC),
    CONSTRAINT [FK_Table_1_Female] FOREIGN KEY ([RecommendTo]) REFERENCES [dbo].[Female] ([ID]),
    CONSTRAINT [FK_Table_1_Male] FOREIGN KEY ([Recommend]) REFERENCES [dbo].[Male] ([ID])
);

RecommendTo 是表 ID 的外键Female,Recommend 是表 ID 的外键值Male

我的问题是我想value在列表视图中显示与外键关联的

示例:表在列Recommend 中有多个外键值女性,列列表具有男性表的值。RecommendTorecommend

我想将与 关联的值显示RecommendTo到列表视图中,即 ID 4 在外键表中有多个值,我想将与该单个数字关联的值显示到列表视图中,该怎么做?

4

1 回答 1

1

因此,您想选择 [Male] 或 [Female] 表中特定记录的所有推荐吗?

如果不使用 LINQ TO SQL,而是执行动态查询或编写存储过程,将如下所示(在 [Female] 表中查找记录建议):

SELECT
    m.ID
    ,m.IMAGE
    ,m.NAME
    ,m.AGE
FROM [dbo].[Male] m
INNER JOIN [dbo].[RecommendFemale] rf ON rf.Recommend = m.ID
WHERE ft.RecommendTo = %FEMALE_ID%
于 2013-09-11T18:27:32.670 回答