0
return mDb.query(DATABASE_TABLE, new String[] {rfrom
}, null, null, null, null, null);

下面的查询是通过使用 sqlite 的 rawquery 标签的方式正确的语句。我想要的是我想使用现有 sqlite 的查询将以下查询语句替换为上述查询。反正有没有可以完成的?

select distinct col from 
(
    SELECT classa col FROM school 
    union all 
    SELECT classb col FROM school
) a 
order by col
4

1 回答 1

0

您可以尝试如下使用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);
}

更多详情请访问链接

于 2013-01-18T11:50:29.380 回答