要求是找到所有与书籍具有相同主题代码的书籍,标题为“华特迪士尼艺术”,使用表格连接而不是子查询,如下所示我中写道。 我面临的问题是,我不太确定如何在不使用子查询的情况下根据所需条件过滤结果。这是我到目前为止所拥有的
SELECT ISBN, Title, Subject_Code
FROM Book
where Subject_Code = Book.subject_Code
ORDER BY Title;
这是我的创建声明,用于阐明我正在使用的表格类型。谢谢!
CREATE TABLE IF NOT EXISTS subject
(SubjectCode nvarchar(3) PRIMARY KEY NOT NULL,
Subject nvarchar(15));
CREATE TABLE IF NOT EXISTS Book
(ISBN nvarchar(13) PRIMARY KEY NOT NULL, Title nvarchar(50), Author nvarchar(30),
Publisher nvarchar(30),
Subject_Code nvarchar(3),
Shelf_Location nvarchar(7), Fiction bit(1),
FOREIGN KEY (Subject_Code) REFERENCES subject(SubjectCode));
答案 0 :(得分:2)
在SQL中,您可以自己加入表。考虑以下内容:
SELECT b2.ISBN, b2.Title, b2.Subject_Code
FROM Book b1
LEFT JOIN Book b2 ON b1.Subject_Code = b2.Subject_Code
WHERE b1.Title = "The Art of Walt Disney"
答案 1 :(得分:2)
您可以使用自联接来执行此操作:
select b.*
from book b join
book bwd
on b.subject_code = bwd.subject_code
where bdw.title = 'The Art of Walt Disney'
order by title;
条件可以在on
子句或where
子句中。