简体   繁体   中英

How can I include rows with a column value greater than what is returned in a SQL query that utilizes inner joins?

I have three tables:

Users

id          Username
---------------------------
1     |     John
2     |     Mark

Positions

id          Position          Authority          Department 
---------------------------------------------------------------
1     |     Supervisor    |       3                  4
2     |     Clerk         |       4                  2    
3     |     Supervisor    |       3                  2
4     |     Admin         |       4                  4
5     |     Assistant     |       5                  2

UserPositions

user_id          position_id 
----------------------------
1               |     1 
2               |     3 

The tables Users and Positions are related to each other using the UserPositions table, which connects them via the id (a foreign key) of each table.

I'm trying to get results back that include anything greater than or equal to the authority and department a user is assigned to. So far, I was able to work out this query:

SELECT A.Username, B.Position, B.Authority, B.Department FROM UserPositions C
LEFT JOIN Users A on C.user_id = A.Id
LEFT JOIN Positions B on C.position_id = B.Id

Which returns:

Username         Position          Authority          Department 
---------------------------------------------------------------
John        |     Supervisor   |      3           |       4
Mark        |     Supervisor   |      3           |       2

However, I want to include positions for that user that are in the same department but also greater than the authority that the user was assigned. For example, John has been assigned the position Supervisor which is in department 4 and authority 3. So I want to return a list of all positions that are in department 4 and have authority of 3 or greater and have it listed next to John. The same for Mark. Like this:

Username         Position          Authority          Department 
---------------------------------------------------------------
John        |     Supervisor   |      3           |       4
John        |     Admin        |      4           |       4
Mark        |     Supervisor   |      3           |       2
Mark        |     Clerk        |      4           |       2
Mark        |     Assistant    |      5           |       2

How can I modified my query above to get this result?

I think that's another join:

select u.username, p1.position, p1.authority, p1.department 
from userpositions   up
inner join users     u  on u.id = up.user_id
inner join positions p  on p.id = up.position_id
inner join positions p1 on p1.department = p.department and p1.authority >= p.authority

Notes:

  • I turned the left join s to inner join ; nothing in your problem statement indicates that you want the former

  • Meaningful table aliases make the queries easier to follow

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM