2011-11-24 48 views
0

使用MySQL,我如何查詢3級深度表中的列?

我的意思是:
[main table] --->[child table 1] --->[child table 2]
> C#1's ID> C#2's ID -------- ------------ > String Column

例如:

[交易]
- ID
- BOOKID *
- 最新
- 員工

[]
- ID
- AUTHORID *
- 標題

[作者]
- ID
- 命名

通過只知道transaction.id,那麼我如何查詢包含以下列的結果?
.. transaction.datebook.titleauthor.name ..(MySQL)查詢具有多級表的多列?

回答

1
select t.date,b.title,a.name from book b 
innerjoin transaction t on t.bookid = b.id 
innerjoin author a on b.authorid = a.id 
+0

太好了!和簡單。 –

0
select transaction.date , book.title , author.name 
from transaction 
    join book on transaction.bookid=book.id 
    join author on book.authorid=author.id 
where transaction.id=<id>; 

查看更多關於加入這裏http://dev.mysql.com/doc/refman/5.5/en/join.html

0

使用MySQL join語法。

SELECT transaction.date , book.title , author.name 
FROM transaction 
    LEFT OUTER JOIN book ON transaction.bookid=book.id 
    LEFT OUTER JOIN author ON book.authorid=author.id 
WHERE transaction.id={your_transaction_id} 

我使用OUTER JOIN因爲,即使書或作者從數據庫中刪除返回交易。在這種情況下,結果將如下所示:'2011-01-12',NULL,NULL