0

I have a table 'Staff' with details of all Staff. 'StaffID' included.

Then I have a table 'StaffRole' which has 'StaffID' and 'RoleID'

I want to do something like this: Select * From Staff Where RoleID=1;

But I'm not sure if I can do this as RoleID is not in the Staff table.

Anyone know the correct syntax? Any feedback is appreciated.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user3074612
  • 99
  • 4
  • 9
  • possible duplicate of [How can an SQL query return data from multiple tables](http://stackoverflow.com/questions/12475850/how-can-an-sql-query-return-data-from-multiple-tables) – Maciej Cygan Mar 16 '14 at 01:21

2 Answers2

2

Try this:

SELECT * FROM staff s
WHERE EXISTS(
   SELECT 'ROLE'
   FROM staffrole r
   WHERE r.staffid = s.staffid
   AND r.roleid = 1
)

In alternative:

SELECT * FROM staff s
JOIN staffrole r
ON r.staffid = s.staffid
WHERE r.roleid = 1
Joe Taras
  • 15,166
  • 7
  • 42
  • 55
2

Use a join:

select s.*
from staffrole r
join staff s
  on s.staffid = r.staffid
where roleid = 1

An index on staffrole(roleid) should make it perform better.

Bohemian
  • 412,405
  • 93
  • 575
  • 722