php – 如何连接两个具有多个公共字段的表用于搜索过滤器

我有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

我正在尝试以这样的方式实现搜索过滤器:当有人点击团队下拉列表时,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>';
    }
}

当我们点击团队过滤器时,URL看起来像这样 – 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)
点赞