我讀的書SQL反模式,並試圖瞭解構建「樹」與自我指涉表其例子如一張桌子與外部連接:這是如何工作的?
Comments
-------------------------------------------------------------------
comment_id | parent_id | author | comment
-------------------------------------------------------------------
1 NULL Fran What's the cause of this bug?
2 1 Ollie I think it's a null pointer.
3 2 Fran No, I checked for that.
4 1 Kukla We need to check for invalid input.
5 4 Ollie Yes, that's a bug.
6 4 Fran Yes, please add a check.
7 6 Kukla That fixed it.
所以這本書
你說可以retrive評論和使用 相對簡單的查詢,它的直接孩子:
SELECT c1.*, c2.* FROM Comments c1 LEFT OUTER JOIN Comments c2 ON c2.parent_id = c1.comment_id
我想了解這是如何工作的。我一直明白左外方式表之間的連接t1
和t2
的是,你需要從和t1
爲行中的所有行,其中ON
條款不滿意你NULL
填寫從第二個表中的列。在這種情況下,只有一個表,但我可以想象查詢發生在兩個表中,其中第二個表是第一個表的副本。不過,我不明白如何查詢返回
樹的兩個級別。
結果表格究竟是什麼,你可以引導我瞭解它是如何產生的邏輯?