如何使用查询转换和简化sql表?

wnrlj8wa  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(362)

这个问题在这里已经有了答案

mysql将行透视为动态列数(1个答案)
9个月前关门了。
编辑:添加我对其执行完全联接的原始表
练习台:

+---------+-----------+------------+------------+
| Column1 |  Column2  |  Column3   |  Column4   |
+---------+-----------+------------+------------+
| 1_e_id  | Chest     | Strength   | Situps     |
| 2_e_id  | Arms      | Strength   | Pushups    |
| 3_e_id  | Full Body | Cardio     | Running    |
| 4_e_id  | Full Body | Stretching | Gynmastics |
| 5_e_id  | Full Body | Cardio     | Swimming   |
| 6_e_id  | Legs      | Strength   | Leg Pulls  |
| 7_e_id  | Full Body | Resting    | Rest       |
| 8_e_id  | Legs      | Strength   | Lunges     |
| 9_e_id  | Arms      | Stretching | Stretching |
| 10_e_id | Legs      | Strength   | Raises     |
+---------+-----------+------------+------------+

训练表:

+-----------+---------+---------+--------------------+
|  Column1  | Column2 | Column3 |      Column4       |
+-----------+---------+---------+--------------------+
| 1_w_e_id  | 1_w_id  | 1_e_id  | Strength Workout   |
| 2_w_e_id  | 1_w_id  | 2_e_id  | Strength Workout   |
| 3_w_e_id  | 1_w_id  | 4_e_id  | Strength Workout   |
| 4_w_e_id  | 2_w_id  | 3_e_id  | Stretching Workout |
| 5_w_e_id  | 2_w_id  | 6_e_id  | Stretching Workout |
| 6_w_e_id  | 2_w_id  | 7_e_id  | Stretching Workout |
| 7_w_e_id  | 2_w_id  | 9_e_id  | Stretching Workout |
| 8_w_e_id  | 3_w_id  | 2_e_id  | Glutes Workout     |
| 9_w_e_id  | 3_w_id  | 5_e_id  | Glutes Workout     |
| 10_w_e_id | 4_w_id  | 6_e_id  | Energy Workout     |
| 11_w_e_id | 4_w_id  | 8_e_id  | Energy Workout     |
| 12_w_e_id | 4_w_id  | 10_e_id | Energy Workout     |
+-----------+---------+---------+--------------------+

完全加入:where exercise.column1=workout.column3
sql查询让我头晕目眩,我搞不懂这个。我使用了完全连接并选择了以下内容:

+---------+------------+----------------------+----------------------+
| Column1 |  Column4   |   Workouts.Column2   |   Workouts.Column4   |
+---------+------------+----------------------+----------------------+
| 4_e_id  | Gymnastics | 1_w_id               | Strength Workout     |
| 1_e_id  | Situps     | 1_w_id               | Strength Workout     |
| 2_e_id  | Pushups    | 1_w_id               | Strength Workout     |
| 6_e_id  | Leg Pulls  | 2_w_id               | Stretching Workout   |
| 9_e_id  | Stretching | 2_w_id               | Stretching Workout   |
| 7_e_id  | Rest       | 2_w_id               | Stretching Workout   |
| 3_e_id  | Running    | 2_w_id               | Stretching Workout   |
| 5_e_id  | Swimming   | 3_w_id               | Glutes Workout       |
| 2_e_id  | Pushups    | 3_w_id               | Glutes Workout       |
| 10_e_id | Raises     | 4_w_id               | Energy Workout       |
| 8_e_id  | Lunges     | 4_w_id               | Energy Workout       |
| 6_e_id  | Leg Pulls  | 4_w_id               | Energy Workout       |
+---------+------------+----------------------+----------------------+

其中,#u e_id是锻炼id(第4列),#u w_id是锻炼id(workouts.column4)。
我想把它带到下面,这里的练习是按锻炼来组织的。

+--------------------+------------+---------+------------+---------+
|      Column1       |  Column2   | Column3 |  Column4   | Column5 |
+--------------------+------------+---------+------------+---------+
| Strength Workout   | Gymnastics | Pushups | Situps     |         |
| Stretching Workout | Leg Pulls  | Rest    | Stretching | Running |
| Glutes Workout     | Swimming   | Pushups |            |         |
| Energy Workout     | Raises     | Lunges  | Leg Pulls  |         |
+--------------------+------------+---------+------------+---------+

在这一点上,如何通过sql查询或者更确切地说,子查询来实现这一点?

cotxawn7

cotxawn71#

如果不将结果放在单独的列中,只需获取同一行中的值,就可以使用group\u concat。。

select  Workouts.Column4, group_conca(your_table.Column4)
    from your_table 
    group by  Workouts.Column4

相关问题