简体   繁体   中英

SQL Select All in Table that have same ID as '..'

Basically, I have a table of support users, and there are different user's of different levels in different departments.

So let's say I basically have the following table:

id | userID | deptID | level
1      1        1        1
2     119       1        2
3      2        1        3
4     101       2        1
5     104       2        2

And I have the id number, so let's say, I want to get all users with the same deptID as the user in id : 3 , thus returning the first three lines.

What kind of SQL statement would that be?

You can use the following query, that contains a subquery:

SELECT * 
FROM <table> 
WHERE deptID=(
              SELECT deptID FROM <table> WHERE userID=3
             )
select  u.*
from    users u join users u2 on u.deptID = u2.deptID
where   u2.id = 3
SELECT *
FROM Users
WHERE deptID IN 
    (SELECT deptID FROM Users WHERE userID = 3)

try this

select my.*
from myTable my
join myTable myt on my.deptID = myt.deptID
where myt.id = 3
select * from data where deptid in (select deptid from data where id=3);

使用此查询

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