This is the example query I would obtain from OrmLite
SELECT *
FROM “LRAANALISI”
INNER JOIN “LRDANALISI” ON (“LRAANALISI”.“IDAANALISI” = “LRDANALISI”.“IDDANALISI”)
INNER JOIN “LRDRISULTATI” ON (“LRAANALISI”.“DANALISIID” = “LRDRISULTATI”.“DANALISIID”)
LEFT JOIN “LRARISULTATI” ON (“LRAANALISI”.“IDAANALISI” = “LRARISULTATI”.“AANALISIID”
AND LRAANALISI.IDAANALISI = LRDRISULTATI.DANALISIID
AND LRARISULTATI.DRISULTATOID = LRDRISULTATI.IDDRISULTATO)
as I cannot do q.LeftJoin<t1, t2, t3> I append multiple LeftJoin<> but the sql I obtain doesn’t execute:
SELECT *
FROM “LRAANALISI”
INNER JOIN “LRDANALISI” ON (“LRAANALISI”.“IDAANALISI” = “LRDANALISI”.“IDDANALISI”)
INNER JOIN “LRDRISULTATI” ON (“LRAANALISI”.“DANALISIID” = “LRDRISULTATI”.“DANALISIID”)
LEFT JOIN “LRARISULTATI” ON (“LRAANALISI”.“IDAANALISI” = “LRARISULTATI”.“AANALISIID”)
LEFT JOIN “LRDRISULTATI” ON (“LRARISULTATI”.“DRISULTATOID” = “LRDRISULTATI”.“IDDRISULTATO”)
LEFT JOIN “LRDRISULTATI” ON (“LRAANALISI”.“DANALISIID” = “LRDRISULTATI”.“DANALISIID”)
the LEFT JOIN should be:
LEFT JOIN “LRARISULTATI” ON (“LRAANALISI”.“IDAANALISI” = “LRARISULTATI”.“AANALISIID”
AND LRAANALISI.IDAANALISI = LRDRISULTATI.DANALISIID
AND LRARISULTATI.DRISULTATOID = LRDRISULTATI.IDDRISULTATO)
Note: the AND LRAANALISI.IDAANALISI = LRDRISULTATI.DANALISIID on the last LEFT JOIN is odd since it doesn’t refer to either of the tables being joined.
You are right, the AND is redundant, I apologize, many hours spent on this today.
But in spite of this I think moving part of the join to the where cut off some records from the result set.
When LRARISULTATO doesnt match any LRAANALISI the where cut off all unmatched LRAANALISI from the result set.
These are the two queries, the first is what I would, the second is what your code generate.
(I took away the redundant AND from both).
SELECT *
FROM “LRAANALISI”
INNER JOIN “LRDANALISI” ON (“LRAANALISI”.“IDAANALISI” = “LRDANALISI”.“IDDANALISI”)
INNER JOIN “LRDRISULTATI” ON (“LRAANALISI”.“DANALISIID” = “LRDRISULTATI”.“DANALISIID”)
LEFT JOIN “LRARISULTATI” ON (“LRAANALISI”.“IDAANALISI” = “LRARISULTATI”.“AANALISIID”
AND LRARISULTATI.DRISULTATOID = LRDRISULTATI.IDDRISULTATO
)
SELECT *
FROM “LRAANALISI”
INNER JOIN “LRDANALISI” ON (“LRAANALISI”.“IDAANALISI” = “LRDANALISI”.“IDDANALISI”)
INNER JOIN “LRDRISULTATI” ON (“LRAANALISI”.“DANALISIID” = “LRDRISULTATI”.“DANALISIID”)
LEFT JOIN “LRARISULTATI” ON (“LRAANALISI”.“IDAANALISI” = “LRARISULTATI”.“AANALISIID”)
WHERE (“LRARISULTATI”.“DRISULTATOID” = “LRDRISULTATI”.“IDDRISULTATO”)
When I execute these statements I get all the expected records from the first but not from the latter.
This new support is available from v4.5.9 that’s now available on MyGet
To show you different customization options possible in OrmLite I’ve also added different examples of how you can achieve a similar result with the current v4.5.8 version of OrmLite by varying level of custom SQL:
var q = db.From<Table1>()
.Join<Table2>((t1, t2) => t1.Id == t2.Id)
.CustomJoin("LEFT JOIN Table3 ON (Table1.Id = Table3.Id AND Table3.Id = Table2.Id)");
var results = db.Select(q);
Using Custom SQL
var results = db.SqlList<Table1>(@"SELECT Table1.*
FROM Table1
INNER JOIN Table2 ON (Table1.Id = Table2.Id)
LEFT JOIN Table3 ON (Table1.Id = Table3.Id AND Table3.Id = Table2.Id)");