SQL show where value is current user or column value of current user

42 Views Asked by At

Currently I am working on a project, and I got stuck to show my results. Please can someone help me out.

I have two tables, Jobs and Employees

Jobs                                     Employees
| JobID    | JobDescr | EmployeeID |     | EmpID    | EmpName  | EmpSub     |
| -------- | -------- | ---------- |     | -------- | -------- | ---------- |
| 1        | Job text | 0005       |     | 0001     | Name 1   | 0003       |
| 2        | Job text | 0008       |     | 0002     | Name 2   | 0004       |
| 3        | Job text | 0003       |     | 0003     | Name 3   | 0001       |
| 4        | Job text | 0004       |     | 0004     | Name 4   | 0004       |
| 5        | Job text | 0001       |     | 0005     | Name 5   | 0001       |

I want to show the job results of the current user (EmID 0001) and his substitute.

Results of current user (EmpID 0001)
| JobID    | JobDescr | EmpName |
| -------- | -------- | ------- |
| 1        | Job text | Name 5  |
| 3        | Job text | Name 3  |
| 5        | Job text | Name 1  |

This is what I tried so far

SELECT JobID, JobDescr e.EmpName FROM Jobs 
LEFT OUTER JOIN Employees AS e ON Jobs = EmployeeID
WHERE EmployeeID = {USERID} AND ... 
ORDER BY JobID, EmployeeID

How to continue? Please help and thanks!

1

There are 1 best solutions below

1
On

If you want jobs by a particular user and by their subs, you can use UNION ALL to combine two queries. This isn't the only way to do it, but it's clean.

select * from jobs where employeeid = {USERID}
union all
select * from jobs where employeeid IN (SELECT empsub FROM employees where empid = {USERID})