简体   繁体   中英

SQL/PHP: Joins with no matches on the other table (multiple tables)

I have four tables:

Personal_trainer(personaltrainerID, name, age, location)
Client(clientID, name, age, location)
Nutrition_Plan(NutritionplanID, personaltrainerID, clientID)
Training_Plan(TrainingplanID, personaltrainerID, clientID)

I'm trying to display that when a personal trainer creates a nutrition/training plan and assigns it to a client, only their specific clients appear as a result, but their is no foreign key in the client table to identify the coach (due to the criteria of the project)

I'm wondering what is the SQL for the neccessary join on both the training/nutrition plan. I have been trying for quite some time and here is my sample code.

Desired output is all the client data IF ONLY they have been assigned either a training or nutrition plan by that specific trainer

In the statement I'm having issues with the Bind parameter so that only users with clients can see their clients. If i use a specified ID i can get a return!

    <?php 
            //code to search for a item from the database
            // user can enter any character to search for a value from the db
           if (isset($_POST['search']))
           {
               $valueToSearch = $_POST['ValueToSearch'];
               $query = "select * from client WHERE concat(`clientID`, `name`, `age`, `sex`, `weight`, `height`, `yearsExperience`, `goal`, `injuries`, 'email')like'%".$valueToSearch."%'";
               $search_result = filterTable($query);

           }
           else {
           $query = "select *
            from client
            where clientID in (select clientID from nutrition_plan where personaltrainerID=?)
            or clientID in (select clientID from training_plan where personalTrainerID=?)";
           $query->bind_param("i", $_POST[""]);
               $search_result = filterTable($query);
           }
           //code to filter the db
           function filterTable($query)
           {
               $connect = mysqli_connect("localhost:3308","root","","fypdatabase");
               $filter_Result = mysqli_query($connect, $query);
               return $filter_Result;
           }
           ?>

           <?php
           while($row = mysqli_fetch_array($search_result))
          { //display the details from the db in the table with option to delete or update entry 
            ?>
                    <tr>
                    <td><?php echo $row["clientID"]; ?></td>
                    <td><?php echo $row["name"]; ?></td>
                    <td><?php echo $row["age"]; ?></td>
                    <td><?php echo $row["sex"]; ?></td>
                    <td><?php echo $row["weight"]; ?></td>
                    <td><?php echo $row["height"]; ?></td>
                    <td><?php echo $row["yearsExperience"]; ?></td>
                    <td><?php echo $row["goal"]; ?></td>
                    <td><?php echo $row["injuries"]; ?></td>
                    <td><?php echo $row["email"]; ?></td>
                    <td> 
                        <a href="?Delete=<?php echo $row["clientID"]; ?>" onclick="return confirm('Are you sure?');">Delete</a>
                    </td>
                    <td>
                        <a href="updateClient.php?Edit=<?php echo $row["clientID"]; ?>" onclick="return confirm('Are you sure?');">Update</a>
                    </td>
                  </tr>
                  <?php

Let's see if I understand your request correctly ...

Show only clients that have a nutrition or training plan by trainer 123:

select *
from client
where clientid in (select clientid from nutrition_plan where personaltrainerid = 123)
   or clientid in (select clientid from training_plan where personaltrainerid = 123);

Show these clients along with all their plans (regardless of the plans' trainers):

select *
from client
join
(
  select 'nutrition' as kind, nutritionplanid as id, personaltrainerid, clientid
  from nutrition_plan
  union all
  select 'training' as kind, trainingplanid as id, personaltrainerid, clientid
  from training_plan
) plan using (clientid)
where clientid in (select clientid from nutrition_plan where personaltrainerid = 123)
   or clientid in (select clientid from training_plan where personaltrainerid = 123);
SELECT name, age, location FROM Client
INNER JOIN
(
  SELECT personaltrainerID, clientID from Nutrion_Plan 
  UNION DISTINCT 
  SELECT personaltrainerID, clientID from Training_Plan
) u
USING(clientID)
WHERE u.personaltrainerID = ?;

这应该工作

SELECT * from client JOIN nutrition_plan ON client.clientid=nutritionplan.clientid JOIN Personaltrainer ON Personaltrainer.personaltrainerID=nutritionplan.personaltrainerID Where Personaltrainer.personaltrainerID="id"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM