SQL/PHP:在另一个 table 上没有匹配的情况下加入(多个 tables)
SQL/PHP: Joins with no matches on the other table (multiple tables)
我有四个table:
Personal_trainer(personaltrainerID, name, age, location)
Client(clientID, name, age, location)
Nutrition_Plan(NutritionplanID, personaltrainerID, clientID)
Training_Plan(TrainingplanID, personaltrainerID, clientID)
我试图表明,当私人教练创建 nutrition/training 计划并将其分配给客户时,结果只会显示他们的特定客户,但他们在客户中没有外键 table 确定教练(由于项目的标准)
我想知道 training/nutrition 计划中必要加入的 SQL 是什么。我已经尝试了很长一段时间,这是我的示例代码。
期望的输出是所有客户数据,前提是该特定培训师已为他们分配了培训或营养计划
在声明中,我遇到了绑定参数问题,因此只有拥有客户端的用户才能看到他们的客户端。如果我使用指定的 ID,我可以获得 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
这应该有效
SELECT * from client JOIN nutrition_plan ON client.clientid=nutritionplan.clientid JOIN Personaltrainer ON Personaltrainer.personaltrainerID=nutritionplan.personaltrainerID Where Personaltrainer.personaltrainerID="id"
让我们看看我是否正确理解了您的要求...
仅显示有教练 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 = ?;
我有四个table:
Personal_trainer(personaltrainerID, name, age, location)
Client(clientID, name, age, location)
Nutrition_Plan(NutritionplanID, personaltrainerID, clientID)
Training_Plan(TrainingplanID, personaltrainerID, clientID)
我试图表明,当私人教练创建 nutrition/training 计划并将其分配给客户时,结果只会显示他们的特定客户,但他们在客户中没有外键 table 确定教练(由于项目的标准)
我想知道 training/nutrition 计划中必要加入的 SQL 是什么。我已经尝试了很长一段时间,这是我的示例代码。
期望的输出是所有客户数据,前提是该特定培训师已为他们分配了培训或营养计划
在声明中,我遇到了绑定参数问题,因此只有拥有客户端的用户才能看到他们的客户端。如果我使用指定的 ID,我可以获得 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
这应该有效
SELECT * from client JOIN nutrition_plan ON client.clientid=nutritionplan.clientid JOIN Personaltrainer ON Personaltrainer.personaltrainerID=nutritionplan.personaltrainerID Where Personaltrainer.personaltrainerID="id"
让我们看看我是否正确理解了您的要求...
仅显示有教练 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 = ?;