【发布时间】:2019-05-07 22:10:00
【问题描述】:
我有一张带列的手机表
- 身份证
- 品牌名称
- 型号名称
- 条件
示例表:
id brand_name model_name condition sell_status
1 Apple 6s New unsold
2 Apple 6s Used unsold
3 Apple 6s New unsold
4 Apple 5s New sold
我希望结果像
brand_name model_name Quantity New Used
Apple 6s 3 2 1
我试过了,但没有得到“新”和“用过”的商品结果:
public List<Mobile> getBrandMobileModel(String brand_name) {
List<Mobile> mobileData = new ArrayList<Mobile>();
String selectAllQuery = "SELECT brand_name, model_name, count(*), count(condition = ?), count(condition = ?) FROM " + Table_Mobile_Details + " WHERE brand_name = ? AND sell_status = ? GROUP BY model_name";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectAllQuery, new String[]{"New", "Used", brand_name, "unsold"});
if (cursor.moveToFirst()) {
do {
Mobile mobile = new Mobile();
//mobile.setId(cursor.getInt(0));
mobile.setBrand_name(cursor.getString(1));
mobile.setModel_name(cursor.getString(2));
mobile.setQuantity(cursor.getString(3));
mobile.setCondition_new(cursor.getString(4));
mobile.setCondition_used(cursor.getString(5));
mobileData.add(mobile);
} while (cursor.moveToNext());
}
return mobileData;
}
【问题讨论】:
-
您的数据库未规范化。这可能是一个严重的问题。
标签: java android sql sqlite android-sqlite