繁体   English   中英

SQL / PHP:在另一个表(多个表)上不匹配的联接

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

我有四个表:

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

我试图显示的是,当私人教练创建营养/培训计划并将其分配给客户时,结果只会显示其特定客户,但是在客户表中它们不是识别教练的外键(由于项目的标准)

我想知道用于培训/营养计划的必要联接的SQL是什么。 我已经尝试了一段时间,这是我的示例代码。

所需的输出是所有客户数据,前提是该培训师仅为其分配了培训计划或营养计划

在语句中,Bind参数存在问题,因此只有具有客户端的用户才能看到其客户端。 如果我使用指定的ID,我可以获得退货!

    <?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

让我们看看我是否正确理解了您的要求...

仅显示由培训师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);

向这些客户展示他们的所有计划(无论计划的培训师如何):

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"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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