【发布时间】:2019-05-29 21:45:31
【问题描述】:
我有四张桌子:
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
【问题讨论】:
-
“如果他们被分配了培训/营养计划” AND 或 OR?
-
您可能想要一个简单的带有
USING(xxxID)子句的INNER JOIN链。 dev.mysql.com/doc/refman/8.0/en/join.html -
或者!对不起,我做出了改变!
-
您的查询中有错误
SELECT * FROM然后LEFT JOIN有完整的条件,并在使用WHERE进行过滤后 -
@Quasimodo'sclone 谢谢!我该怎么写?
标签: php mysql sql join outer-join