Php 我可以在内部联接子句之前包含where子句吗
我有这个密码Php 我可以在内部联接子句之前包含where子句吗,php,mysql,Php,Mysql,我有这个密码 $q = $conn->query("SELECT facultydetails.F_NAME,Paper_title from faculty inner join facultydetails on faculty.Fac_ID = facultydetails.Fac_ID where (paper_path = 'NULL' OR paper_path = '') and (certificate_path = 'NULL' OR certificate_path
$q = $conn->query("SELECT facultydetails.F_NAME,Paper_title from faculty inner join facultydetails on faculty.Fac_ID = facultydetails.Fac_ID where (paper_path = 'NULL' OR paper_path = '') and (certificate_path = 'NULL' OR certificate_path = '') and (report_path = 'NULL' OR report_path = '') " );
现在我需要添加一个条件,其中用户将提供id,并且只有与id相关的内容应该被选中,我才能使用此代码
$q = $conn->query("SELECT facultydetails.F_NAME,Paper_title from faculty where facultydetails.Fac_ID='$FacID' inner join facultydetails on faculty.Fac_ID = facultydetails.Fac_ID where (paper_path = 'NULL' OR paper_path = '') and (certificate_path = 'NULL' OR certificate_path = '') and (report_path = 'NULL' OR report_path = '') " );
使用php时,不能在内部联接之前添加where子句
SELECT facultydetails.F_NAME,Paper_title
from faculty
inner join facultydetails on faculty.Fac_ID = facultydetails.Fac_ID
where facultydetails.Fac_ID='$FacID'
AND (paper_path = 'NULL' OR paper_path = '') and (certificate_path = 'NULL' OR certificate_path = '') and (report_path = 'NULL' OR report_path = '')
内部联接的语法如下所示:
SELECT column_list
FROM t1
INNER JOIN t2 ON join_condition1
INNER JOIN t3 ON join_condition2
...
WHERE where_conditions;
不能在内部联接之前添加where,但可以将条件添加到on子句中,如:
"SELECT facultydetails.F_NAME,Paper_title ".
"from faculty ".
"inner join facultydetails ".
"on (facultydetails.Fac_ID='$FacID') and (faculty.Fac_ID = facultydetails.Fac_ID) ".
"where (paper_path = 'NULL' OR paper_path = '') and (certificate_path = 'NULL' OR certificate_path = '') and (report_path = 'NULL' OR report_path = '') "
或者,您可以对教员表进行预筛选,如:
"SELECT facultydetails.F_NAME,Paper_title ".
"from (select * from faculty where (facultydetails.Fac_ID='$FacID')) f ".
"inner join facultydetails ".
"on f.Fac_ID = facultydetails.Fac_ID ".
"where (paper_path = 'NULL' OR paper_path = '') and (certificate_path = 'NULL' OR certificate_path = '') and (report_path = 'NULL' OR report_path = '') "
编辑:
在SQL中,如果代码为“上方”或“下方”,则单个查询中的某些其他部分将不能保证在它之前执行。一个SQL只能有一个where子句,您可以如上所述使用它。我希望这对您有用。