如何获得科目先决条件的名称?

时间:2012-05-30 14:32:58

标签: sql sql-server

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

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

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

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 个答案:

答案 0 :(得分: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

答案 1 :(得分: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方法的工作原理here。我的答案进一步改进,指出here

另见SQL Server - Possible Pivot Solution?

答案 2 :(得分: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's answer这样的CTE就是你要走的路。但是,我建议使用SQL对n层应用程序来说是错误的。

答案 3 :(得分: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