這是非常奇怪的查詢,我不知道如何處理它。 以下是表格。如何爲下表編寫postgres查詢?
id descendentId attr_type attr_value
1 {4} type_a
2 {5} type_a
3 {6} type_a
4 {7,8} type_b
5 {9,10} type_b
6 {11,12} type_b
7 {} type_x TRUE
8 {} type_y "ABC"
9 {} type_x FALSE
10 {} type_y "PQR"
11 {} type_x FALSE
12 {} type_y "XYZ"
輸入相關的查詢將是1,2,3
..輸出應該"ABC"
。
邏輯是 - 從1,2,3
開始通過後代ID循環,直到達到attr_type x
。如果attr_type x
達到了7,9和11,那麼檢查哪一個是true
。對於例如7
是真的,那麼 得到它的兄弟type_y
(檢查第4行),這是8
並返回它的值。
所有這些都是字符串格式。
'descendentId'的數據類型是什麼? –
如果你保存一個'parentId'而不是這樣(這樣,你甚至可以使用一個自引用外鍵)。 – pozs