I have a website that allows coaches to assign and track athletes training. For the athletes to see their training 4 tables need to work together, I last updated this years ago as only I and my athletes was using it and I used to just do another sql request inside the while loop of the previous request to get the desired result, as others will be using it in the future I want to do things properly and I don't think this is possible with prepared statements.
I am not sure if what I have is a coding problem or indeed a logic problem.
The tables are:
Group - This is used to track which athletes are in which groups or teams, in_group being the id of the users
groupName | in_group |
---|---|
group1 | 1,2,3,4 |
group2 | 5,6,7,8 |
Training - This is used to assign the training, and track which athletes can make each session, plan being the id of the work out in workout_title and attending being the id of the users
sessDate | groupName | plan | attending |
---|---|---|---|
2021-09-05 | group1 | 1,2,3 | 2,4 |
2021-07-08 | group2 | 4,5,6 | 6,7,8 |
Workout_title - this holds the titles of the work outs for the athletes, the idea is the coach can have multiple set workouts and adjust things like sets, reps etc
id | groupName | wo_title |
---|---|---|
1 | group1 | warm up |
2 | group1 | plyos |
4 | group2 | sprints |
Workout_exercise - this holds the individual exercises of the work outs, linked by the tid column which is the workout_title id
tid | wo_title | exercise_name |
---|---|---|
1 | warm up | high knees |
1 | warm up | A-skip |
2 | plyos | bounds |
2 | plyos | hops |
4 | sprints | 40m |
4 | sprints | 30m |
4 | sprints | 20m |
I have tried JOIN
and INNER JOIN
(which I think are the same thing?) for the 1st 3 tables all seemed to work fine except it only returns the 1st workout in the plan column. I can't figure out a way for the sql request to run through all the id's in the plan column and line out the corresponding workout_title.
$ps = $db_conx->prepare("SELECT group.groupName, training.sessDate,
training.plan, workout_title.id, workout_title.wo_title, workout_title.groupName
FROM group
JOIN training
ON group.groupName = training.groupName
JOIN workout_title
ON training.plan = workout_title.id
WHERE FIND_IN_SET(?, in_group)");
$ps->bind_param("s", $username);
$ps->execute();
$result = $ps->get_result();
while ($row = $result->fetch_assoc()) {
}
I read some solutions as to storing the results and then running another sql request but again could not get this to work, the result would not match.
If there is no way to cade it and indeed I need to re arrange the tables any help with the logic in doing that would also be appreciated.