我有一个像这样的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
我不知道如何加入这些表来获得所需的结果。我该怎么做?
答案 0 :(得分: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
答案 1 :(得分: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
答案 2 :(得分: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,否则你需要添加一个外键。