I have a situation where in i need to pull data from one table but exclude some rows based on the rows in another table. I mean that i need to pull studentid(s) from one table but exclude those studentid(s) which are there in another table.
first query :
$sql = "select studentid from table 2 where iarsid = '12'";
as i’ll get an array result from this query i want to use this result and put it in NOT conditions in the next query simply excluding these very rows from the result from this another query.
Second query:
$sql2 = "select studentid from table 2, table 3 where iarsid = '12' // and a lot of joins";
Basically the students who are in the first table are not needed while pulling out students based on the second query.
If i am using the wrong logic, please guide so as to achieve this.
You can do the general idea at least 3 ways, using a LEFT JOIN, and also using NOT IN and NOT EXISTS.
Via LEFT JOINS.
This gets all student information in table_A, where the student is not in table_B.
and here it is via NOT EXISTS:
and via NOT IN