Multiple Join syntax in SQLike

215 Views Asked by At

I'm trying to construct a two level JOIN in SQLike, and can't get past the stage of a one level JOIN.

The JSONs I use as source tables are:

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"}]; 

The MySQL equivalent of what I'm trying to do is:

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

The first level JOIN works great:

 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}
})

But when I try to add the second JOIN, I get no results. The syntax I use is this:

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}

})

Any ideas on how to get it right?

1

There are 1 best solutions below

0
On BEST ANSWER

On and Join properties will be overridden. There's no good documentation and code is unreadable, but you can try implicit join or this:

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