我可以在内部联接子句之前包含where子句吗

时间:2019-06-26 11:11:28

标签: 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 = '') 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

3 个答案:

答案 0 :(得分:2)

不,您不能在内部联接之前添加where子句。

内部联接的语法如下:

SELECT column_list
FROM t1
INNER JOIN t2 ON join_condition1
INNER JOIN t3 ON join_condition2
...
WHERE where_conditions;

答案 1 :(得分:0)

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 = '')

答案 2 :(得分:0)

您不能在内部联接之前添加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中,在单个查询中其他部分位于代码“上方”或“下方”的情况下,将不能保证在此代码之前先执行该代码。