加入多对一,但显示为一项结果

时间:2018-06-22 10:54:08

标签: android sql sqlite

我必须在要加入的SQLite中创建表。一个表具有ID和练习标题。另一张桌子上有该练习的图像。每次锻炼可以有很多图像。

我尝试使用JOIN编写SQL,但是每次练习给我2行(因为每次练习至少要有图像行)。但是,我希望将其放在一行上,因为我将在Android Java适配器中使用结果。

我将在下面将我的帖子发布到表以及我拥有的SQL中。如果可以使用除JOIN之外的其他方法解决此问题,我还将发布Java方法。

运动索引:

╔════════════╦═══════════════╗
║ 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

1 个答案:

答案 0 :(得分:0)

如果只有两个图像,则可以进行有条件的聚集:

select exercise_id, exercise_title, 
       max(case when seq = 1 then exercise_image_id end) as exercise_image_id_1,
       max(case when seq = 1 then exercise_image_file end) as exercise_image_file_1,
       max(case when seq = 2 then exercise_image_id end) as exercise_image_id_2,
       max(case when seq = 2 then exercise_image_file end) as exercise_image_file_2
from (select *, row_number() over (partition by ei.exercise_id order by eimg.exercise_image_id) seq
      from exercise_index ei inner join
           exercise_index_images eimg
           on eimg.exercise_image_exercise_id = ei.exercise_id
     ) t
group by exercise_id, exercise_title;