1

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

$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

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175

3 Answers3

2

No you can not add the where clause before the inner join.

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;
Shivani Sonagara
  • 1,299
  • 9
  • 21
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 = '')
Claudio
  • 5,078
  • 1
  • 22
  • 33
0

You cannot add a where before the inner join, but you can add your condition into the on clause, like:

"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.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175