【发布时间】:2018-12-01 20:07:09
【问题描述】:
我必须在 SQLite 中加入我想要加入的表。一张表有练习的 ID 和标题。另一个表有该练习的图像。每个练习可以有许多图像。
我尝试使用 JOIN 编写 SQL,但每次练习它给我 2 行(因为每个练习至少有图像)。但是我想要它在一行,因为我将在 Android Java 适配器中使用结果。
我将在下面发布我的表以及我拥有的 SQL。我还将发布 Java 方法,以防万一这可以通过使用 JOIN 以外的其他方法解决。
exercise_index:
╔════════════╦═══════════════╗
║ exercise_id║ exercise_title║
╠════════════╬═══════════════╣
║ 1 ║ Bench press ║
║ 2 ║ Sit ups ║
║ 3 ║ Push ups ║
╚════════════╩═══════════════╩
exercise_index_images:
╔═══════════════════╦═══════════════════════════╦════════════════════╗
║ exercise_image_id ║ exercise_image_exercise_id║ exercise_image_file║
╠═══════════════════╬═══════════════════════════╬════════════════════╣
║ 1 ║ 1 ║ bench_press_1.png ║
║ 2 ║ 1 ║ bench_press_2.png ║
║ 3 ║ 2 ║ sit_ups_1.png ║
║ 4 ║ 2 ║ sit_ups_2.png ║
║ 5 ║ 3 ║ push_ups_1.png ║
║ 6 ║ 3 ║ push_ups_2.png ║
╚═══════════════════╩═══════════════════════════╩════════════════════╩
我现在的查询:
String query = "SELECT exercise_index.exercise_id, exercise_index.exercise_title, " +
"exercise_index_images.exercise_image_id, exercise_index_images.exercise_image_file " +
"FROM exercise_index " +
"JOIN exercise_index_images ON exercise_index.exercise_id=exercise_index_images.exercise_image_exercise_id";
这给了我:
╔════════════╦═══════════════╗╔═══════════════════╦═══════════════════════════╦════════════════════╗
║ exercise_id║ exercise_title║║ exercise_image_id ║ exercise_image_exercise_id║ exercise_image_file║
╠════════════╬═══════════════╣╠═══════════════════╬═══════════════════════════╬════════════════════╣
║ 1 ║ Bench press ║║ 1 ║ 1 ║ bench_press_1.png ║
║ 1 ║ Bench press ║║ 2 ║ 1 ║ bench_press_2.png ║
║ 2 ║ Sit ups ║║ 3 ║ 2 ║ sit_ups_1.png ║
║ 2 ║ Sit ups ║║ 4 ║ 2 ║ sit_ups_2.png ║
║ 3 ║ Push ups ║║ 5 ║ 3 ║ push_ups_1.png ║
║ 3 ║ Push ups ║║ 6 ║ 3 ║ push_ups_2.png ║
╚════════════╩═══════════════╩╚═══════════════════╩═══════════════════════════╩════════════════════╩
我想要的是这样的:
╔════════════╦═══════════════╗╔═════════════════════╦═════════════════════════════╦══════════════════════╗╔═════════════════════╦═════════════════════════════╦══════════════════════╗
║ exercise_id║ exercise_title║║ exercise_image_id_1 ║ exercise_image_exercise_id_1║ exercise_image_file_1║║ exercise_image_id_2 ║ exercise_image_exercise_id_2║ exercise_image_file_2║
╠════════════╬═══════════════╣╠═════════════════════╬═════════════════════════════╬══════════════════════╣╠═════════════════════╬═════════════════════════════╬══════════════════════╣
║ 1 ║ Bench press ║║ 1 ║ 1 ║ bench_press_1.png ║║ 2 ║ 1 ║ bench_press_2.png ║
║ 2 ║ Sit ups ║║ 3 ║ 2 ║ sit_ups_1.png ║║ 4 ║ 2 ║ sit_ups_2.png ║
║ 3 ║ Push ups ║║ 5 ║ 3 ║ push_ups_1.png ║║ 6 ║ 3 ║ push_ups_2.png ║
╚════════════╩═══════════════╩╚═════════════════════╩═════════════════════════════╩══════════════════════╩╚═════════════════════╩═════════════════════════════╩══════════════════════╩
Android/Java 方法:
public void populateExercises(){
/* Database */
DBAdapter db = new DBAdapter(this);
db.open();
// Get all food for that category
String currentLanguageSQL = db.quoteSmart(currentLanguage);
String query = "SELECT exercise_index._id, exercise_index.exercise_id, exercise_index.exercise_title, exercise_index.exercise_type_id " +
"FROM exercise_index " +
"WHERE exercise_language=" + currentLanguageSQL + " AND exercise_muscle_group_id_main=" + currentMuscleGroupMainId;
query = query + " ORDER BY exercise_title ASC";
listCursor = db.rawQuery(query);
// Find ListView to populate
ListView lvItems = findViewById(R.id.listViewExercises);
// Setup cursor adapter using cursor from last step
ExercisesCMuscleGroupsOpenMainCursorAdapter exercisesAdapter = new ExercisesCMuscleGroupsOpenMainCursorAdapter(this, listCursor);
// Attach cursor adapter to the ListView
try {
lvItems.setAdapter(exercisesAdapter); // uses ContinensCursorAdapter
} catch (Exception e) {
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
}
// Close db
db.close();
// OnClick
lvItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
exerciseListItemClicked(arg2);
}
});
} // populateExercises
【问题讨论】: