2017-02-17 151 views
0

我試圖TU使用數組內的AlaSQL Array agregator,這似乎是不支持:(AlaSQL嵌套數組

試圖acomplish此輸出(或這樣):

[ 
    { 
     "keyword":"project 1", 
     "projects": [ 
      { 
       "year":2014, 
       "description":"this is a project description", 
       "budget": 5600, 
       "status": "closed", 
       "metadata": [ 
        { 
         "key":"provider", 
         "value":"just me" 
        }, 
        { 
         "key":"client", 
         "value":"someone" 
        }, 
        { 
         "key":"any thing", 
         "value":"any data..." 
        } 
       ] 
      } 
     ] 
    } 
] 

我的數據庫結構

people e 
| name  | type | 
|-----------|---------| 
| name  | varchar | 
| lastname | varchar | 
| person_id | varchar | 
| cel  | int  | 

projects p 
| name  | type | 
|-------------|---------| 
| keyword  | varchar | 
| year  | int  | 
| description | text | 
| budget  | int  | 

PeopleProjects x 
| name  | type | 
|-------------|---------| 
| e_person_id | varchar | 
| p_keyword | varchar | 
| p_year  | int  | 
| status  | varchar | 

metadata m 
| name  | type | 
|-------------|---------| 
| e_person_id | varchar | 
| p_keyword | varchar | 
| p_year  | int  | 
| key   | varchar | 
| value  | varchar | 

我查詢

alasql("SELECT p.keyword, \ 
ARRAY({year:p.year, description:p.description, budget:p.budget, status:x.status, \ 
    ARRAY({key:m.key, value:m.value}) as metadata \ 
}) AS projects \ 
FROM projects p, PeopleProjects x, metadata m \ 
WHERE \ 
    p.keyword = x.p_keyword AND x.p_keyword = m.p_keyword AND \ 
    p.year = x.p_year AND x.p_year = m.p_year \ 
    x.e_person_id = x.e_person_id AND AND x.e_person_id = 2 \ 
GROUP BY p.keyword"); 

此刻,我分了查詢兩,然後結合數據產生孔的東西。

任何想法?

回答

0

我設法獲取兩次得到我想要的輸出:

首先,項目:

var projects = alasql("SELECT p.keyword, ARRAY({status:x.status, year:p.year, description:p.description, budget:p.budget }) AS years \ 
FROM PeopleProjects x, projects p \ 
WHERE x.p_keyword = p.keyword AND x.p_year = p.year AND x.e_person_id = "+person_id+" \ 
GROUP BY p.keyword"); 

二,元數據:

var mdata = alasql("SELECT m.e_person_id, m.p_keyword, m.p_year, ARRAY({key:m.key,value:m.value}) as [metadata] \ 
FROM metadata m, PeopleProjects x \ 
WHERE x.e_person_id = m.e_person_id AND x.p_keyword = m.p_keyword AND x.p_year = m.p_year AND x.e_person_id = "+person_id+" \ 
GROUP BY m.e_person_id, m.p_keyword, m.p_year"); 

然後,遍歷項目,搜索和存儲所述元數據:

for(i=0;i<projects.length;i++){ 
    for(j=0;j<projects[i].years.length;j++){ 
     current = projects[i].keyword; 
     current_year = projects[i].years[0].year; 
     search = alasql("SEARCH/* WHERE(year="+current_year+" AND keyword='"+current+"') FROM ?",[mdata]); 

     projects[i].years[j]["metadata"] = search[0].metadata; 
    } 
} 

現在projects包含所有數據。

任何建議將受到歡迎。