我有3个mySQL InnoDB表:
图书
A_ID smallint(5)无符号自动增量主键
标题varchar(60)
作者
B_ID smallint(5)无符号自动增量主键
名称varchar(30)
Author_Book 多对多表格
A_ID smallint(5)无符号
B_ID smallint(5)无符号
主键(A_ID,B_ID)
FOREIGN KEY(A_ID)参考作者(A_ID)
FOREIGN KEY(B_ID)REFERENCES书籍(B_ID)
我不理解如何将LEFT JOIN与多对多表格一起使用:比如,向每本书的作者展示。我尝试了这个看起来合乎逻辑但没有运气的查询:
SELECT Books.Title, Authors.Name
FROM Books LEFT JOIN ON Books.B_ID = Author_Book.B_ID
LEFT JOIN ON AuthorBook.A_ID = Authors.A_ID;
我离开基地还是在我的查询中有标点符号/语法错误?
任何帮助将不胜感激。
答案 0 :(得分:0)
SELECT Books.Title, Authors.Name
FROM Books LEFT JOIN Author_Book ON Books.B_ID = Author_Book.B_ID
LEFT JOIN Authors ON AuthorBook.A_ID = Authors.A_ID;
答案 1 :(得分:0)
我建议您尝试使用内部联接作为第二个,但问题是您错过了要加入的表:
SELECT Books.Title, Authors.Name FROM Books LEFT JOIN Author_Book ON Books.B_ID = Author_Book.B_ID LEFT JOIN Authors ON AuthorBook.A_ID = Authors.A_ID;
或者,如果您不需要没有书籍的作者:
SELECT Books.Title, Authors.Name FROM Books INNER JOIN Author_Book ON Books.B_ID = Author_Book.B_ID INNER JOIN Authors ON AuthorBook.A_ID = Authors.A_ID;