SQLike中的多个Join语法

时间:2012-01-30 08:45:11

标签: javascript json sqlike

我正在尝试在SQLike中构建一个两级JOIN,并且无法超越一级JOIN的阶段。

我用作源表的JSON是:

var placesJSON=[{"id":"173","name":"England","type":"SUBCTRY"},{"id":"580","name":"Great Britain","type":"CTRY"},{"id":"821","name":"Southern England","type":"REG"},{"id":"822","name":"Northern England","type":"REG"},{"id":"831","name":"Southwest England","type":"REG"},{"id":"832","name":"Southeast England","type":"REG"},{"id":"833","name":"Western Midlands","type":"REG"},{"id":"834","name":"Eastern Midlands","type":"REG"},{"id":"889","name":"Eastern England","type":"REG"},{"id":"937","name":"Central Southern England","type":"REG"}];
var relationsJSON=[{"son":"173","father":"580"},{"son":"821","father":"173"},{"son":"822","father":"173"},{"son":"831","father":"173"},{"son":"832","father":"173"},{"son":"833","father":"173"},{"son":"834","father":"173"},{"son":"889","father":"173"},{"son":"937","father":"173"}]; 

我正在尝试做的MySQL相当于:

SELECT DISTINCT p.id, p.name, p.type, r.son, r.father, f.name  
FROM places p
JOIN places_relations r ON p.id=r.son
JOIN places f ON f.id=r.father

第一级JOIN效果很好:

 SQLike.q({
   SelectDistinct: ['p_id', 'p_name', 'r_son' ,'p_type', 'r_father'],
   From: {p:placesJSON},
   Join: {r:relationsJSON},  
   On: function(){return this.p.id==this.r.son}
})

但是当我尝试添加第二个JOIN时,我没有得到任何结果。我使用的语法是:

SQLike.q({
   SelectDistinct: ['p_id', 'p_name', 'r_son' ,'p_type', 'r_father', 'f_name'],
   From: {p:placesJSON},
   Join: {r:relationsJSON},  
   On: function(){return this.p.id==this.r.son},
   Join: {f:placesJSON},  
   On: function(){return this.f.id==this.r.father}

})

关于如何做到正确的任何想法?

1 个答案:

答案 0 :(得分:0)

将覆盖On和Join属性。没有好的文档和代码是不可读的,但你可以试试implicit join或者这个:

Join: {r:relationsJSON, f:placesJSON},  
On: function(){return this.p.id==this.r.son && this.f.id==this.r.father; }