0

好的,我有这样的来源

public List<SearchRecord> getResult() {
        List<SearchRecord> searchResult = new ArrayList<SearchRecord>();
        String query = "SELECT " + A + ", " + B+ ", " + C
                + " FROM " + TABLE_A+ " UNION ALL SELECT " + A+ ", "
                + B+ ", " + C+ " FROM " + TABLE_B               + " UNION ALL SELECT " + A+ ", " + B+ ", "
                + C+ " FROM " + TABLE_C + " UNION ALL SELECT "
                + A+ ", " + B+ ", " + C+ " FROM "
                + TABLE_D;
        Cursor cursor = mDatabase.rawQuery(query, null);
        if (cursor != null) {
            cursor.moveToFirst();
            do {
                SearchRecord sr = new SearchRecord();
                sr.setRecordA(cursor.getString(0));
                sr.setRecordB(cursor.getString(1));
                sr.setRecordC(cursor.getString(2));
                searchResult.add(sr);
            } while (cursor.moveToNext());
            return searchResult;
        }
        return null;
    }

使用此代码,我可以从具有相同列名的多个表中获取一些记录,并将结果存储在 List 对象中。但是我如何找出这条记录属于哪个表?是来自 TABLE_A、TABLE_B、TABLE_C 还是其他

4

1 回答 1

1

根据 SQL,不,您正在连接 3 组行并以相同的方式操作它们,每条记录无法通过其来源来区分。

当然,你可以做类似的事情

String query = "SELECT 'TABLE_A', " + A + ", " + B+ ", " + C
            + " FROM " + TABLE_A+ " UNION ALL SELECT 'TABLE_B' " + A+ ", "
            + B+ ", " + C+ " FROM " + TABLE_B 
            + " UNION ALL SELECT 'TABLE_C', " + A+ ", " + B+ ", "
            + C+ " FROM " + TABLE_C + " UNION ALL SELECT 'TABLE_D', "
            + A+ ", " + B+ ", " + C+ " FROM "
            + TABLE_D;

:-)

于 2013-06-18T08:47:32.597 回答