Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Query, join returning a list

Tags:

sql

join

I have three tables:

  • tblEmployee

    employeeId int PK

    name varchar(50)

  • tblSkill

    skillId int PK

    name

  • tblEmployeeSkill

    employeeSkill int PK

    employeeId int FK

    skillId int FK

I want to run a query that will return all of the skills and whether a particular user has them. ie:

SQL| no

C# | yes

ruby | no

where userId = 1

any help please?

like image 720
Andy Stannard Avatar asked Aug 16 '26 11:08

Andy Stannard


1 Answers

A simple LEFT JOIN against the list of skills should suffice for this. If the employee does not have an entry matching that skill, the primary key for that entry will be NULL:

SELECT tblSkill.name, 
       CASE 
         WHEN tblEmployeeSkill.employeeSkill IS NULL THEN 'no' 
         ELSE 'yes' 
       END as hasSkill
FROM tblSkill
LEFT JOIN tblEmployeeSkill 
  ON tblSkill.skillId = tblEmployeeSkill.skillId 
  AND tblEmployeeSkill.employeeId = 1
like image 155
PinnyM Avatar answered Aug 18 '26 03:08

PinnyM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!