简体   繁体   English

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

[英]Can I include a where clause before Inner join clause

I have This code 我有这个代码

$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 = '') " );

Now I need to add a condition where a user will give the id and only That id related stuff should be picked can I use this code 现在,我需要添加一个条件,在该条件下,用户将提供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 = '') " ); 

Working in php 用PHP工作

No you can not add the where clause before the inner join. 不,您不能在内部联接之前添加where子句。

Syntax for the inner join is as below: 内部联接的语法如下:

SELECT column_list
FROM t1
INNER JOIN t2 ON join_condition1
INNER JOIN t3 ON join_condition2
...
WHERE where_conditions;
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 = '')

You cannot add a where before the inner join, but you can add your condition into the on clause, like: 您不能在内部联接之前添加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 = '') "

or you can prefilter your faculty table, like: 或者您可以预过滤您的教职员工表,例如:

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

EDIT: 编辑:

In SQL having a code "above" or "below" some other part inside a single query will not guarantee that it is executed before it. 在SQL中,在单个查询中的其他部分“上面”或“下面”的代码将无法保证它先执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM