2013-10-29 72 views
0

我有一個表「BinaryTree」,它有2列「CustomerID」和「ParentID」。sql查詢獲取Spacetree數據(jit)

另一臺是「客戶」,它有列「」客戶ID」和‘名字’。

我想從這些表中查詢數據,並想JSON格式分配這些數據Spacetree。

請有下面的鏈接的參考: -

http://philogb.github.io/jit/static/v20/Jit/Examples/Spacetree/example1.code.html

http://philogb.github.io/jit/static/v20/Jit/Examples/Spacetree/example1.html

我想要的數據如下所示: -

Parentid CustomerID FirstName 
    1   34   Test1 
    1   64   Test2 
    1   46   Test3 
    34   45   Test4 
    34   102  Test5 
    64   22   Test6 
    46   54   Test7 

因此,我可以構建JSON字符串並將其分配給spacetree。 如果它按順序返回數據,我會很好,它意味着它首先返回所有的孩子。

之後,它會逐個返回這些childerns的子節點,因此很容易以spacetree想要的適當格式構建json字符串。

如果需要更多信息,請讓我知道。

在此先感謝你們。

回答

1

使用內部連接。

select BT.PARID,BT.CUSTID,CU.firstname 
from BinaryTree BT INNER JOIN Customers CU on BT.CUSTID=CU.CUSTID 
ORDER BY BT.PARID,BT.CUSTID 

檢查演示:http://sqlfiddle.com/#!3/1ffc1/1

+0

感謝@Vijay,這是給結果如下http://www.evernote.com/shard/s364/sh/3ca8dee4-8157-4531-87b9-fb910df6709c/1faff5c73dba42c0d3301f3d1a94062e,因爲第1個有2個customerid(意味着2個孩子),所以它的第一個customerid是3,所以我想要在第4行的第7和第8行以及第5和第6行的下面parentid 3.希望你能幫上忙。 – AnandMeena

+0

@AnandMeena:更新答案,按順序添加custid –

+0

坦克@Vijay,現在工作正常:-) – AnandMeena