如果在带有内连接的sql查询中的语句

时间:2017-04-16 02:58:57

标签: php mysql if-statement inner-join

对不起,我知道论坛上有很多关于这个问题的答案,但我没有成功地回答我的问题。

这是我的功能:

public function getPupil($db, $pupil_id){
    $pupil = $db->query('
        SELECT 
        a.gender as genderPupil,
        pupil_name, 
        pupil_lastname,
        a.id as pupilId, 
        b.id as idOne, 
        c.id as idTwo, 
        b.lastname as lastnameOne, 
        b.name as nameOne, 
        c.name as nameTwo, 
        c.lastname as lastnameTwo,
        birthdate,
        school,
        level,
        allergies,
        special_diet,
        tap,
        d.name as tapNameFirstTerm,
        e.name as tapNameSecondTerm,
        f.name as tapNameThirdTerm

        FROM pupils a
        INNER JOIN responsibles b ON a.id_responsibles = b.id
        INNER JOIN responsibles c ON a.id_responsibles_two = c.id
        INNER JOIN tap_activities d ON a.tap_first_term = d.id
        INNER JOIN tap_activities e ON a.tap_second_term = e.id 
        INNER JOIN tap_activities f ON a.tap_third_term = f.id
        WHERE a.id = ?
        ', [$pupil_id])->fetch();
    return $pupil;
}

当a.tap_first_term或a.tap_second_term或a.tap_third_term等于0时,我想添加不查询INNER JOIN的条件。

非常感谢您的回答。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。 对于可能感兴趣的人,我只是通过LEFT JOIN更改了tap_activities的三个INNER JOIN。它可以按照我的意愿运作。