[英]How to join two tables with multiple common fields for search filter
我有2個表bpi_registration和bpi_teamProfile。 bpi_registration中的字段是: id, id_school, first_name,last_name,email,city,state,country
和bpi_teamProfile表中的字段是id_team, team_name, id_student1, id_student2, id_student3
now id_student1,id_student2,id_student3包含相同的學生id
在bpi_registration中。 我不知道如何連接多個字段。 我寫的查詢如下。 如果我錯了,請糾正我:
SELECT * FROM bpi_registration
INNER JOIN bpi_teamProfile
ON bpi_registration.id=bpi_teamProfile.id_student1
AND bpi_registration.id=bpi_teamProfile.id_student2
AND bpi_registration.id=bpi_teamProfile.id_student3
AND bpi_registration.id=bpi_teamProfile.id_student4
AND bpi_registration.id=bpi_teamProfile.id_student5
我正在嘗試以這樣一種方式實現搜索過濾器:當有人點擊團隊下拉列表時firstname,lastname,email,city,state,country
bpi_registration
的firstname,lastname,email,city,state,country
bpi_registration
顯示出來。 下面是我的PHP代碼
if (isset($_GET['Team'])) { $sql="SELECT * FROM bpi_registration INNER JOIN bpi_teamProfile ON bpi_registration.id=bpi_teamProfile.id_student1 AND bpi_registration.id=bpi_teamProfile.id_student2 AND bpi_registration.id=bpi_teamProfile.id_student3 AND bpi_registration.id=bpi_teamProfile.id_student4 AND bpi_registration.id=bpi_teamProfile.id_student5" $userQuery = "{$sql} WHERE bpi_teamProfile.team_name = :team_id"; $user = $db->prepare($userQuery); $user->execute(['team_id' => $_GET['Team']]); $selectedUser=$user->fetch(PDO::FETCH_ASSOC); if(isset($selectedUser)) { echo '<tr>'; echo '<td>' . $selectedUser['first_name'] . '</td>'; echo '<td>' . $selectedUser['last_name'] . '</td>'; echo '<td>' . $selectedUser['email'] . '</td>'; echo '<td>' . $selectedUser['address_city'] . '</td>'; echo '<td>' . $selectedUser['address_state'] . '</td>'; echo '<td>' . $selectedUser['address_country'] . '</td>'; echo '</tr>'; } }
當我們點擊團隊過濾器時,網址看起來像這樣 - https://www.example.com/retrieve1.php?Grade=&School=&Team=mary+winners&Students=
但是我無法獲得理想的結果。
似乎不太可能學生在給定行中的id列都映射回相同的id(在注冊中)......或者查詢將正確返回。 如果它們不同,是否要加入'OR'?
SELECT * FROM bpi_registration
INNER JOIN bpi_teamProfile
ON (bpi_registration.id=bpi_teamProfile.id_student1)
OR (bpi_registration.id=bpi_teamProfile.id_student2)
OR (bpi_registration.id=bpi_teamProfile.id_student3)
OR (bpi_registration.id=bpi_teamProfile.id_student4)
OR (bpi_registration.id=bpi_teamProfile.id_student5)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.