问题
I need to write following Query in ORMLite
SELECT * FROM T1 t1, T2 t2 where t1.id = t2.id AND t1.type='abc' AND (t1.title = 'XYZ' OR t2.description = 'xyz');
However So Far I have been able to write following code:
QueryBuilder<T1, Integer> t1QB = getT1Dao().queryBuilder();
QueryBuilder<T2, Integer> t2QB = getT2Dao().queryBuilder();
t1QB.join(t2QB);
Where<T1, Integer> where = t1QB.where();
where.eq("Type", "abc");
where.and().or(
where.ne("title", "XYZ"),
where.ne("description", "xyz"),
);
but this throws exception that column not found "description" in T1. And also T1 has T2 object in it and autoRefresh is true in @DatabaseField annotation.
Is there is any way to do this with above method or I have to write custom Query
回答1:
For people with same problem
I have resolved this problem with change in query
SELECT * FROM T1 WHERE type='abc' AND (title = 'XYZ' OR id IN (SELECT id from T2 where description = 'xyz'));
QueryBuilder<T1, Integer> t1QB = getT1Dao().queryBuilder();
QueryBuilder<T2, Integer> t2QB = getT2Dao().queryBuilder();
t2QB.where().eq(id, id);
Where<T1, Integer> where = t1QB.where();
where.or(
where.ne("title", "XYZ"),
where.in("id", t2QB);
);
where.and().eq("Type", "abc");
来源:https://stackoverflow.com/questions/27294725/fetch-data-from-multiple-table-ormlite