在php中使用带有多个左连接的WHERE子句

时间:2017-07-24 09:35:31

标签: php mysql

我使用 PHP 成功完成了LEFT JOIN,但是,添加WHERE子句让我从所有现有详细信息中选择唯一记录时遇到一些困难找到匹配的表格。

这是我的代码:

  <?php
$sql = "SELECT staff.*, staff_bio.bio_id, staff_cert.*, staff_edu.*, staff_pos.*, staff_res.*\n"
    . " FROM staff staff\n"
    . "LEFT JOIN staff_bio \n"
    . " ON staff.nuc_id = staff_bio.bio_id\n"
    . "LEFT JOIN staff_cert\n"
    . " ON staff.nuc_id = staff_cert.pro_id\n"
    . "LEFT JOIN staff_edu\n"
    . " ON staff.nuc_id = staff_edu.edu_id\n"
    . "LEFT JOIN staff_pos\n"
    . " ON staff.nuc_id = staff_pos.rank_id\n"
    . "LEFT JOIN staff_res\n"
    . " ON staff.nuc_id = staff_res.res_id\n"
    . "WHERE staff.nuc_id = $userRow['staff_no'] ";//this is where i'm having issues
?>

我的最终输出应该是:

<?php echo $userRow['sch_name']; ?>
<?php echo $userRow['fac_name']; ?>
<?php echo $userRow['dep_name']; ?>

所有来自不同的表格。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

你可以这样做:

. " WHERE staff.nuc_id = ".$userRow['staff_no'] ;//this is where i'm having issues

为什么需要连接查询,可以像编写格式一样编写:

<?php 

    $sql = "SELECT staff.*, staff_bio.bio_id, staff_cert.*, staff_edu.*, staff_pos.*, staff_res.*
         FROM staff staff
        LEFT JOIN staff_bio 
         ON staff.nuc_id = staff_bio.bio_id
        LEFT JOIN staff_cert
         ON staff.nuc_id = staff_cert.pro_id
        LEFT JOIN staff_edu
         ON staff.nuc_id = staff_edu.edu_id
        LEFT JOIN staff_pos
         ON staff.nuc_id = staff_pos.rank_id
        LEFT JOIN staff_res
         ON staff.nuc_id = staff_res.res_id
        WHERE staff.nuc_id = ".$userRow['staff_no'];//this is where i'm having issues

     ?>