假设以下表格布局
CREATE TABLE name_area (
_id INTEGER PRIMARY KEY NOT NULL,
name TEXT NOT NULL,
area TEXT NOT NULL,
UNIQUE(name, area)
)
以及以下值
name area
---- ----
Aaron A
Chris A
Bunny A
Ron A
Burgundy B
Helen B
Tim B
假设您想知道 Aaron、Ron 和 Burgundy 是否都在同一个区域:
SELECT COUNT(*), area FROM name_area
WHERE name='Aaron' OR name='Ron' OR name='Burgundy' GROUP BY area
这将返回两行。
2 A
1 B
即其中两个在同一区域(A),一个在另一个(B):
表示为Cursor
你可以这样检查:
Cursor cursor = ...; // Format your query & do the SELECT
try {
if (cursor.moveToNext()) {
int count = cursor.getCount();
if (count < 2) {
// Everyone is in the same area
int n = cursor.getInt(0);
// Now verify 'n' against the number of people you queried for
// if it doesn't match one or more didn't exist in your table.
} else {
// People are in different areas
int n = 0;
do {
n += cursor.getInt(0);
} while (cursor.moveToNext());
// Now verify 'n' against the number of people you queried for
// if it doesn't match one or more didn't exist in your table.
}
} else {
// Oops nothing was found.
}
} finally {
cursor.close();
}