user3608814
user3608814

Reputation: 571

SQLite LeftJoin

With this query I get the data from two tables. Unfortunately, if the "Table2" there are no records, are not extracted even those of the "Table 1". How can I get the data of the "Table 1" even if the "Table2" does not have the corresponding data?

String tabella_conti = "SELECT a._id, " +
    "a.CAMPO1, " +
    "a.CAMPO2, " +
    "a.CAMPO3, " +
    "SUM(b.ZAMPO1) - SUM(b.ZAMPO2), " +         
    "b.ZAMPO3 " +       
    "FROM Table1 a LEFT JOIN MyTable b ON (a.CAMPO1  =  ZAMPO3) WHERE "+MyTable.DATA+ " <=? GROUP BY a.CAMPO1";

Upvotes: 0

Views: 36

Answers (1)

wvdz
wvdz

Reputation: 16641

Make the where clause part of the join clause by replacing it with AND

SELECT (...)
FROM Table1 a
LEFT JOIN MyTable b
ON a.CAMPO1  =  ZAMPO3
AND (...)
GROUP BY (...)

This ensures that no rows from table1 will be excluded.

Upvotes: 2

Related Questions