0

我有一张order这样的桌子

id  | bookId | bookAuthorId
--------------------------
1      3         2      
2      2         1        
3      1         2        

和另一张桌子

bookId  |  book
---------------
  1       bookA
  2       bookB
  3       bookC

bookAuthorId  |  author
------------------------
  1              authorA
  2              authorB

我想从具有这样结果集的order表中获取记录id = 1

id | book | author

我尝试了什么:

select * from order 
join bookId,bookAuthorId 
   on order.bookId = books.bookId 
       and order.authorId = authors.authorId

我不知道如何加入这些表以获得所需的结果。我该怎么做?

4

3 回答 3

3
select o.id, b.book, a.author
from 'order' o
join book b on o.bookid=b.bookid
join author a on o.bookauthorid=a.bookauthorid 
where o.id=1
于 2013-06-27T22:04:15.440 回答
2

您可以使用该where子句来做到这一点

select 
    id, book, author 
from 
    `order`, book, author 
where 
    `order`.bookId = book.bookId 
    and 
    `order`.authorId = author.authorId

或者

select 
    o.id, b.book, a.author
from 
    `order` o 
natural join
    book b
natural join
    author a
于 2013-06-27T21:58:15.707 回答
2
select `order`.id, book.book, author.author
from `order`
join book on (`order`.bookId = book.bookId)
join author on (author.bookAuthorId = book.bookId)
where `order`.id = 1;

假设 bookAuthorId 可以链接到 bookId,否则您需要添加外键。

于 2013-06-27T21:58:24.533 回答