SQL/PHP:在另一个表(多个表)上不匹配的联接
我有四张桌子:SQL/PHP:在另一个表(多个表)上不匹配的联接,php,mysql,sql,join,outer-join,Php,Mysql,Sql,Join,Outer Join,我有四张桌子: Personal_trainer(personaltrainerID, name, age, location) Client(clientID, name, age, location) Nutrition_Plan(NutritionplanID, personaltrainerID, clientID) Training_Plan(TrainingplanID, personaltrainerID, clientID) <?php
Personal_trainer(personaltrainerID, name, age, location)
Client(clientID, name, age, location)
Nutrition_Plan(NutritionplanID, personaltrainerID, clientID)
Training_Plan(TrainingplanID, personaltrainerID, clientID)
<?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
我试图表明,当私人教练创建营养/培训计划并将其分配给客户时,结果只有他们的特定客户出现,但由于项目的标准,客户表中没有外键来识别教练
<?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
我想知道,在培训/营养计划中,必要加入的SQL是什么。我已经尝试了很长时间,下面是我的示例代码
<?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
期望输出是所有客户数据,前提是该特定培训师为他们分配了培训或营养计划
<?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
在语句中,我对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
这应该行得通
<?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
选择*from client JOIN nutrition\u plan ON client.clientid=nutritionplan.clientid JOIN Personaltrainer ON personaltrainerID=nutritionplan.personaltrainerID Where Personaltrainer.personaltrainerID=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提供营养或培训计划的客户展示:
<?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
where clientid in (select clientid from nutrition_plan where personaltrainerid = 123)
or clientid in (select clientid from training_plan where personaltrainerid = 123);
向这些客户展示他们的所有计划,而不考虑计划的培训师:
<?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
(
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);
如果他们被分配了一个训练/营养计划和/或?您可能需要一个简单的内部连接链,使用xxxid子句。或对不起,我来找钱!您的查询中有一个错误,请选择“从中选择*”,然后使用完整条件左连接,然后使用WHERE@Quasimodo“谢谢你!我该怎么写呢?谢谢,卡山,快到了!然而,结尾的id也是什么?这就是我的错误所在,personaltrainerID?这实际上应该显示,通过他的个人id或姓名,我得到了你,它不会抛出任何错误并运行,但是表中没有显示任何数据@Darán:这个查询向您提供一位私人教练、他们的营养计划和相关客户。如果按原样进行查询,即按字面上的id进行查询,那么您将查找trainer id 0,因为id表示一个名为id的列,并且由于该列不存在,MySQL默认为字符串“id”,由于这不是一个数字,它将转换为0。看起来不错,谢谢!当pesonaltrainerID来自数据库并且可以是任何数字时,例如,它会是什么而不是“123”?会是“?”来自数据库?这是什么意思?从另一个表中,例如,从personal_trainer中选择personaltrainerid,其中name='John Smith'?然后,您可以简单地用parantesses中的这个子查询替换123:。。。其中personaltrainerid=从personal_trainer中选择personaltrainerid,其中name='John Smith';太好了,谢谢!所以我应该举你的第二个例子,如果这是你想要的。试试看,然后检查结果。查询会为您提供所有客户的所有计划,前提是至少有一个客户的计划由所需的培训师提供。我了解您。谢谢,我尝试了第一个查询,没有出现错误,但表中没有显示客户的输出!谢谢,太好了!!您会用什么代替1,以便在不同的培训师登录时可以使用不同的personaltrainerID?当然是这样的参数?准备好的声明。我什么时候包括?标记本身不返回任何结果,是否应该有另一行用于绑定参数?请看我上面编辑的例子!由于您已将问题标记为属于PHP,我希望您希望使用mysqli或DBO的prepare方法强烈建议这样做,因为将客户端数据直接插入SQL会使您的应用程序容易受到SQL注入攻击。我使用的是bind param,但在I for integer之后,我不知道要传递什么值$查询->绑定参数,$\u POST[];-->不知道邮局在哪里意味着什么