dth96108 2017-07-24 09:35
浏览 83
已采纳

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

I successfully did a LEFT JOIN using PHP, however, I am having a little difficulty adding a WHERE clause to enable me select unique record from all existing details in all the tables where a match is found.

This is my code :

  <?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
?>

My final output should be something like :

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

All coming from different tables. Any help will be greatly appreciated.

  • 写回答

1条回答 默认 最新

  • dongliao8069 2017-07-24 09:38
    关注

    You can do like this:

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

    Why do you need to concatenate query, it can be written as if you want formatting :

    <?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
    
         ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站