我有5张表:
my_books
表:
genre
表:
genre_ind
表:
authors
表:
author_ind
表:
我需要连接表格以按流派ID选择所有书籍信息。由于作者信息存储在其他表中,我需要根据书籍ID添加作者信息。
尝试在单个查询中执行此操作,到目前为止,我有以下查询:
SELECT my_books.id_book,
my_books.title,
my_books.price,
my_books.descrip,
a.genre,
s.authors
FROM my_books
LEFT JOIN genre_ind
ON my_books.id_book = genre_ind.id_book
LEFT JOIN genre a
ON genre_ind.id_genre = a.id_genre
LEFT JOIN author_ind
ON books.id_book=author_ind.id_book
LEFT JOIN authors s
ON author_ind.id_author=s.id_author
WHERE author_ind.id_book=my_books.id_book
AND genre_ind.id_genre=%d
我有功能查询,如果我需要根据类型ID从my_books信息中选择信息,但我尝试将其扩展为选择作者信息,但它不起作用!
更新
现在代码quire看起来像这样:
SELECT my_books.id_book,
my_books.title,
my_books.price,
my_books.descrip,
a.genre,
s.authors
FROM my_books
LEFT JOIN genre_ind
ON my_books.id_book = genre_ind.id_book
LEFT JOIN genre a
ON genre_ind.id_genre = a.id_genre
LEFT JOIN author_ind
ON my_books.id_book = author_ind.id_book
LEFT JOIN authors s
ON author_ind.id_author = s.id_author
WHERE genre_ind.id_genre=%d
我得到的错误是:MySQL:#1054 - '字段列表'中的未知列'suthors'
感谢您的帮助,非常感谢
好的,问题解决了。非常感谢您的时间和帮助!!!!
正确的quire:
SELECT my_books.id_book,
my_books.title,
my_books.price,
my_books.descrip,
a.genre,
s.author
FROM my_books
LEFT JOIN genre_ind
ON my_books.id_book = genre_ind.id_book
LEFT JOIN genre a
ON genre_ind.id_genre = a.id_genre
LEFT JOIN author_ind
ON my_books.id_book = author_ind.id_book
LEFT JOIN authors s
ON author_ind.id_author = s.id_author
WHERE genre_ind.id_genre=%d
答案 0 :(得分:0)
第一个问题是这个on
条款:
ON books.id_book=author_ind.id_book
未定义任何名为books
的内容。
第二个问题是where
子句正在撤消left outer join
的效果。
也许你的意思是:
FROM my_books LEFT JOIN
genre_ind
ON my_books.id_book = genre_ind.id_book LEFT JOIN
genre a
ON genre_ind.id_genre = a.id_genre LEFT JOIN
author_ind
ON my_books.id_book = author_ind.id_book LEFT JOIN
---------^ changed from `books`
authors s
ON author_ind.id_author=s.id_author
WHERE genre_ind.id_genre=%d