Home > OS >  How to not equal multiple table mysql
How to not equal multiple table mysql

Time:08-16

Table_Teacher

id    | name  
-----------------  
1     | Kevin  
2     | Alex   
3     | jax   
4     | Albert

Table_Supervisor

id    | id_teacher  
-----------------   
1     | 1  
2     | 3

I want to display 2 data (Alex & Albert) in table_teacher with queries

SELECT A.name FROM table_teacher A,tbl_supervisor B WHERE  B.id_teacher != A.id;

why is this not working ?

CodePudding user response:

There are various ways.

A simple one is to use a subquery to get all teachers that are not contained in the subquery of supervisor

SELECT A.name FROM table_teacher a where a.id not in 
      (select b.id_teacher from table_supervisor b WHERE b.id_teacher = a.id);

CodePudding user response:

You could use LEFT JOIN, which might perform better than subqueries.

select t.name
from Teacher t 
left join Supervisor s on t.id=s.id_teacher
where s.id_teacher is null;

Above query will get only the values which are on Teacher table but not on Supervisor table

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=98ae9aa420456e69bae05f00ec34eb43

  • Related