Search code examples
c#sqlsql-serversqlkata

How do I define JOIN precedence in SqlKata


I'm using SqlKata in my project and it's necessary to connect several tables with the help of nested join. I expect to see something like that:

SELECT * FROM t1
LEFT JOIN (t2 LEFT JOIN t3 ON t3.id = t2.id) 
ON t2.id = t1.id

In Join/LeftJoin/RigthJoin methods, I did not find any overloads that would accept anything other than a join or other request.

Wouldn't want to manually make such connections, maybe someone has already faced such a problem? That would be great, I would really appreciate a hint.


Solution

  • Defining JOIN precedence is not available in SqlKata at the moment.

    But you can achieve same result by using a Sub Query.

    var query = new Query("t1")
        .LeftJoin(
            new Query("t2").LeftJoin("t3", "t3.id", "t2.id").As("tmp"),
           j => j.On( "tmp.id", "t1.id")
        );
    

    This query would result in the following sql:

    SELECT * FROM [t1] LEFT JOIN (
        SELECT * FROM [t2] LEFT JOIN [t3] ON [t3].[id] = [t2].[id]
    ) AS [tmp] ON ([tmp].[id] = [t1].[id])