Database 从多个表创建视图而不复制输出
我想从3个不同的表中创建一个视图,并且没有重复的输出。我目前的代码似乎正确地组织了主题的书名。但是,它会将每本书的标题打印给同一位作者,打印完成后会转到下一位作者。它不是15行,而是270行Database 从多个表创建视图而不复制输出,database,postgresql,Database,Postgresql,我想从3个不同的表中创建一个视图,并且没有重复的输出。我目前的代码似乎正确地组织了主题的书名。但是,它会将每本书的标题打印给同一位作者,打印完成后会转到下一位作者。它不是15行,而是270行 CREATE VIEW book_summary(author_first_name, author_last_name, book_title, subject) AS SELECT DISTINCT first_name, last_name,title, subject FROM authors,
CREATE VIEW book_summary(author_first_name, author_last_name, book_title, subject)
AS SELECT DISTINCT first_name, last_name,title, subject
FROM authors, books left join subjects
ON subjects.subject_id = books.subject_id;
我试着通过软件查找答案。在这篇文章中,我遇到了DISTINCT和LEFT JOIN。我只是觉得我用的不对
Output:
Andrew | Brookins | 2001: A Space Odyssey | Science Fiction
Andrew | Brookins | Bartholomew and the Oobleck | Children's Books
Andrew | Brookins | Dune | Science Fiction
Andrew | Brookins | Dynamic Anatomy | Arts
Andrew | Brookins | Franklin in the Dark | Children's Books
Andrew | Brookins | Goodnight Moon | Children's Books
Andrew | Brookins | Learning Python | Computers
Andrew | Brookins | Little Women | Drama
Andrew | Brookins | Perl Cookbook | Computers
Andrew | Brookins | Practical PostgreSQL | Computers
Andrew | Brookins | Programming Python | Computers
Andrew | Brookins | The Cat in the Hat | Children's Books
Andrew | Brookins | The Shining | Horror
Andrew | Brookins | The Tell-Tale Heart | Horror
Andrew | Brookins | The Velveteen Rabbit | Classics
(对我在另一个表中的每一位作者都是如此)。当您使用
交叉连接/逗号语法时,会发生这种情况:
CREATE VIEW book_summary
(author_first_name, author_last_name, book_title, subject)
AS
SELECT DISTINCT first_name, last_name,title, subject
FROM authors, books --here is the problem
left join subjects
ON subjects.subject_id = books.subject_id;
应该是:
CREATE VIEW book_summary
(author_first_name, author_last_name, book_title, subject)
AS
SELECT first_name, last_name,title, subject
FROM authors a
JOIN books b
ON a.col_name = b.col_name
left join subjects s
ON s.subject_id = b.subject_id;
谢谢你的救命恩人:D