Skip to content
Advertisement

Exclude all from attributes where employee table does not have connection with in the pivot table

      Attributes   |           Pivot         |     Employees
                   |                         |
      attribute1   |  attribute1|employee1   |     employee1
      attribute2   |  attribute2|employee1   |
      attribute3   |                         |


   

I need to exclude all attributes that have no connection with employee1 in Pivot table. in this case attribute3. What is the SQL query for that?

Advertisement

Answer

Use not exists:

select a.*
from attributes a
where not exists (select 1
                  from pivot p
                  where p.attribute_id = a.attribute_id and
                        p.employee_id = @employee_id
                 );
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement