EDIT: Adding the original tables I did a full join on
Exercise Table:
+---------+-----------+------------+------------+
| 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 |
+---------+-----------+------------+------------+
Workout Table:
+-----------+---------+---------+--------------------+
| 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 |
+-----------+---------+---------+--------------------+
Full join: WHERE Exercise.Column1=Workout.Column3
SQL queries make my head spin and I can't figure this one out. I've used a full join and select to get it down to the following:
+---------+------------+----------------------+----------------------+
| 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 |
+---------+------------+----------------------+----------------------+
where #_e_id is the exercise_id (Column4) and #_w_id is the workout_id (Workouts.Column4).
And I'd like to get it to the following where the exercises are organized by workout.
+--------------------+------------+---------+------------+---------+
| 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 | |
+--------------------+------------+---------+------------+---------+
How can I do this via a SQL query or rather, subquery at this point?