使用以下查询,我从db中的两个表中获取数据。但是如果COL5为空,则不显示(TABLE1)的数据。它还应该显示其他数据?
SQLiteDatabase db = mHelper.getReadableDatabase();
final List<Dettaglio1> dettagli1 = new ArrayList<Dettaglio1>();
String sql = "SELECT C._id, " +
"B.col1, " +
"B.col2, " +
"B.col3, " +
"B.col4, " +
"B.col5, " +
"B.col6, " +
"SUM(C.col01), " +
"C.col02, " +
"C.col03 " +
"FROM table1 B LEFT JOIN table2 C ON (B.col5 = C.col02)";
Cursor cur = db.rawQuery(tabella_conti, null);
while (cur.moveToNext()){
Dettaglio1 d01 = new Dettaglio1();
d01.id= cur.getString(0);
d01.col1= cur.getString(1);
d01.col2= cur.getDouble(2);
d01.col3= cur.getString(3);
d01.col4 = cur.getString(4);
d01.col5= cur.getString(5);
d01.col6 = cur.getString(6);
d01.col01 = cur.getDouble(7);
d01.col02 = cur.getString(8);
d01.col03= cur.getString(9);
dettagli1.add(d01);
}
cur.close();
db.close();
....
....
}
答案 0 :(得分:0)
如果要包含NULL值,则应使用LEFT或RIGHT连接,如下所示:
... from table1 as B RIGHT JOIN table2 as C ON (B.col5 = C.col02)
如果某些平台不支持RIGHT join(感谢@ user3608814,请参阅注释)使用LEFT join并更改表的顺序:
... from table2 as C LEFT JOIN table1 as B ON (C.col02 = B.col5)
更新
如果这没有帮助(当您写完时table2
中没有数据,但仍需要结果),您应重新组织< / strong>您的数据方案:
向table2
添加明确的外键,将其链接到table1
。设为table2.extID
列。然后可以像这样查询:
... from table1 as B LEFT JOIN table2 as C ON (B._ID = C.extID)