[英]Table joins accross multiple database tables and objects in PHP and MySQL
[英]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.