我爲我的應用程序提供了一個內容提供程序。爲了簡單起見,我將用一個例子。我有一個'OrderDetails'表。如何在使用Content Provider時進行GROUP BY?
- 我有一個OrderId列。
- 我有一個在OrderId中購買的不同產品類型數量的列。
- 我有一個OrderDetailId是表的主鍵。
我想寫查詢:SELECT OrderID, sum(quantity) FROM OrderDetails GROUP BY OrderID
這將返回此:
不過,我試圖插入我的GROUP BY子句中我的URI爲我內容提供商但它不會工作,所以得到的查詢變成:SELECT OrderID, sum(quantity) FROM OrderDetails
,返回此(所有內容和最後OrderId的整個數量):
這裏是獲取光標,只需打印出結果,我剛纔提出:
private void findQuantityByOrder(){
Uri uri = DatabaseContract.CONTENT_URI_ORDER_DETAILS;
String[] selection = new String[] {DatabaseContract.DatabaseOrderDetails.ORDER_ID,
"sum("+ DatabaseContract.DatabaseOrderDetails.QUANTITY + ")"
+"GROUP BY(" + DatabaseContract.DatabaseOrderDetails.ORDER_ID + ")"};
String projection = null;
String sortBy = null;
String[] args = null;
Cursor cursor = getContentResolver().query(
uri,
selection,
projection,
args,
sortBy);
for (int i = 0; i < cursor.getCount(); i ++) {
cursor.moveToPosition(i);
int orderID = cursor.getInt(cursor.getColumnIndex("orderID"));
int quantity = cursor.getInt(cursor.getColumnIndex("sum(quantity)"));
System.out.println("orderId: " + orderID + ". Quanitity: " + quantity);
}
}
,只打印出所有訂單的全部資金,與過去的ID,這是在桌子上。
我相信GROUP BY已被刪除,不再支持。有沒有其他方法可以提供相同的結果?
謝謝
你怎麼看如果輸出名爲selection的變量? –