Index: src/where.c ================================================================== --- src/where.c +++ src/where.c @@ -1612,10 +1612,11 @@ pLoop = pLevel->pWLoop; idxCols = 0; for(pTerm=pWC->a; pTermprereq==0 && (pTerm->wtFlags & TERM_VIRTUAL)==0 + && !ExprHasProperty(pTerm->pExpr, EP_FromJoin) && sqlite3ExprIsTableConstant(pTerm->pExpr, pSrc->iCursor) ){ pPartial = sqlite3ExprAnd(pParse->db, pPartial, sqlite3ExprDup(pParse->db, pTerm->pExpr, 0)); } if( termCanDriveIndex(pTerm, pSrc, notReady) ){ Index: test/autoindex4.test ================================================================== --- test/autoindex4.test +++ test/autoindex4.test @@ -46,7 +46,28 @@ SELECT (SELECT count(*) FROM t1, t2 WHERE a=e AND x=f), e, f, '|' FROM t3 ORDER BY rowid; } {1 123 654 | 0 555 444 | 4 234 987 |} + +# Ticket [2326c258d02ead33d] +# Two joins, one with and the other without an ORDER BY clause. +# The one without ORDER BY correctly returns two rows of result. +# The one with ORDER BY returns no rows. +# +do_execsql_test autoindex4-3.0 { + CREATE TABLE A(Name text); + CREATE TABLE Items(ItemName text , Name text); + INSERT INTO Items VALUES('Item1','Parent'); + INSERT INTO Items VALUES('Item2','Parent'); + CREATE TABLE B(Name text); + + SELECT Items.ItemName + FROM Items + LEFT JOIN A ON (A.Name = Items.ItemName and Items.ItemName = 'dummy') + LEFT JOIN B ON (B.Name = Items.ItemName) + WHERE Items.Name = 'Parent' + ORDER BY Items.ItemName; +} {Item1 Item2} + finish_test