-2

这是一个粗略的草图。我有一个先决条件表和主题表。我有一个粗略的想法,我可以列出主题代码。但我真的不确定如何获得可以列出主题名称和详细信息的查询,这是先决条件。

例如,我想编写一个查询,列出主题名称及其先决条件名称。所以结果会是(好吧,我稍后会做连接文本):“计算机简介是操作系统的先决条件”。

我只是想知道如何从这两个表中提取主题名称?

CREATE TABLE subjects (
subject_code VARCHAR(7) NOT NULL CONSTRAINT subject_pk PRIMARY KEY,
subject_name VARCHAR(50) NOT NULL,
subject_details TEXT NOT NULL
);



CREATE TABLE SubjectPrerequisite
(   Primary_Subject_Code        VARCHAR(7) NOT NULL,
    Prerequisite_Subject_Code   VARCHAR(7) NOT NULL,
    CONSTRAINT PK_SubjectPrerequisite PRIMARY KEY (Primary_Subject_Code, Prerequisite_Subject_Code),
    CONSTRAINT FK_SubjectPrerequisite_Primary_Subject_Code FOREIGN KEY (Primary_Subject_Code) REFERENCES Subject (Subject_Code),
    CONSTRAINT FK_SubjectPrerequisite_Prerequisite_Subject_Code FOREIGN KEY (Prerequisite_Subject_Code) REFERENCES Subject (Subject_Code)
)

//编辑:这是我到目前为止所拥有的

SELECT  subject_name
FROM SubjectPreRequisite t0
INNER JOIN subjects t1
ON t0.subject_code = s1.prerequisite_subject_code
4

4 回答 4

1

我假设(可能是错误的)您希望将先决条件的主题名称连接成一行。下面是一个 SQL Server 示例,说明如何做到这一点:

;WITH Prerequisites AS
(   SELECT  Primary_Subject_Code, Subject_Name
    FROM    SubjectPrerequisite
            INNER JOIN Subjects
                ON Subject_Code = Prerequisite_Subject_Code
)           
SELECT  Subject_Code,
        Subject_Name,
        Subject_Details,
        STUFF(  (   SELECT  ',' + Subject_Name
                    FROM    Prerequisites
                    WHERE   Primary_Subject_Code = Subject_Code
                    FOR XML PATH(''), TYPE
                ).value('.', 'VARCHAR(MAX)'), 1, 1, '') AS PrerequisiteList
FROM    Subjects

我之前已经完整解释了 XML PATH 方法是如何工作随着对我的回答的进一步改进,这里指出

另请参阅SQL Server - 可能的 Pivot 解决方案?

于 2012-05-30T14:43:34.460 回答
1

假设您想要主题名称的总列表,请执行以下查询:

select subject_name from subjects

假设您希望主题先决条件和 subject_code 与 Primary_Subject_Code 有关系,请执行以下查询:

select s.subject_name, r.Prerequisite_Subject_Code
from subjects s
inner join SubjectPrerequisite r on s.subject_code = r.Primary_Subject_Code

并与您的连接:

select r.Prerequisite_Subject_Code ' + is a pre-requisite of ' + s.subject_name as 'Pre-Requisites'
from subjects s
inner join SubjectPrerequisite r on s.subject_code = r.Primary_Subject_Code
于 2012-05-30T14:40:38.333 回答
0

如果您只想将 pre-prequsite 关系提取到 1 级递归,您可以这样做

SELECT
              [original].[subject_code] [OriginalCode]
            , [prereq].[subject_code] [Pre-RequisiteCode]
    FROM
            [subject] [orginal]
        LEFT JOIN
            [SubjectPrerequisite] [spr]
                ON [spr].[Primary_Subject_Code] = [original].[subject_code]
        JOIN
            [subject] [prereq]
                ON [prereq].[subject_code] = [spr].[Prerequisite_Subject_Code]
    ORDER BY
             [OriginalCode]
           , [Pre-RequisiteCode]

如果您想显示递归链并以某种方式连接主题,那么像GarethD 的答案这样的 CTE 就是要走的路。但是,我建议使用 SQL 执行此操作对于 n 层应用程序是错误的。

于 2012-05-30T15:09:18.020 回答
0

认为我解决了它:

 SELECT t.subject_name + 'is a pre-requisite of' + s.subject_name
 FROM subjects s
 INNER JOIN pre_requisites r ON s.subject_code = r.subject_code
 INNER JOIN subjects t ON t.subject_code = r.pre_requisite_code
于 2012-05-30T15:38:13.843 回答