在一个查询中选择多个到多个表

时间:2015-08-03 13:06:04

标签: mysql sql

我有桌子:

**RUBRIC:** `RubricID, RubricName`

**AUTOR:** `AutorID, FirstName, LastName`

**BOOK:** `BookID, BookName, book_photo`

**BOOKAUTOR:** `BookID, AutorID`

**BOOKRUBRIC:** `BookID, RubricID`

我想写一个返回BookName,FirstName,LastName,RubricName的SELECT查询。

我有单独的查询,但我不知道如何在一个选择中写这个。 这就是我得到的:

SELECT book.BookName, autor.FirstName, autor.LastName FROM bookautor JOIN book ON book.BookID=bookautor.BookID JOIN autor on autor.AutorID=bookautor.AutorID;

SELECT rubric.RubricName FROM bookrubric JOIN rubric ON rubric.RubricID=bookrubric.RubricID;

它运作良好,但如何在一个选择中写入?

我试试这个,但它不起作用:

SELECT book.BookName, autor.FirstName, autor.LastName, rubric.RubricName FROM bookautor,bookrubric JOIN book ON book.BookID=bookautor.BookID JOIN autor on autor.AutorID=bookautor.AutorID LEFT JOIN rubric ON rubric.RubricID=bookrubric.RubricID;

它返回错误:

ERROR 1054 (42S22): Unknown column 'bookautor.BookID' in 'on clause'

1 个答案:

答案 0 :(得分:0)

尝试此查询

SELECT book.BookName, autor.FirstName, autor.LastName, rubric.RubricName 
FROM bookautor
JOIN book ON book.BookID=bookautor.BookID 
JOIN autor on autor.AutorID=bookautor.AutorID 
left join bookrubric on book.BookID=bookrubric.BookID
LEFT JOIN rubric ON rubric.RubricID=bookrubric.RubricID;