您可以尝试如下使用SQLiteQueryBuilder
:
public void testUnionQuery() {
String expected;
String[] innerProjection = new String[] {"name", "age", "location"};
SQLiteQueryBuilder employeeQueryBuilder = new SQLiteQueryBuilder();
SQLiteQueryBuilder peopleQueryBuilder = new SQLiteQueryBuilder();
employeeQueryBuilder.setTables("employee");
peopleQueryBuilder.setTables("people");
String employeeSubQuery = employeeQueryBuilder.buildUnionSubQuery(
"_id", innerProjection,
null, 2, "employee",
"age=25",
null, null, null);
String peopleSubQuery = peopleQueryBuilder.buildUnionSubQuery(
"_id", innerProjection,
null, 2, "people",
"location=LA",
null, null, null);
expected = "SELECT name, age, location FROM employee WHERE (age=25)";
assertEquals(expected, employeeSubQuery);
expected = "SELECT name, age, location FROM people WHERE (location=LA)";
assertEquals(expected, peopleSubQuery);
SQLiteQueryBuilder unionQueryBuilder = new SQLiteQueryBuilder();
unionQueryBuilder.setDistinct(true);
String unionQuery = unionQueryBuilder.buildUnionQuery(
new String[] { employeeSubQuery, peopleSubQuery }, null, null);
expected = "SELECT name, age, location FROM employee WHERE (age=25) " +
"UNION SELECT name, age, location FROM people WHERE (location=LA)";
assertEquals(expected, unionQuery);
}
更多详情请访问链接