如何用一句sql判断两个员工在同一个部门?

dong03 2003-11-06 10:37:19
ID 部门ID 员工名字

DM_ID Dept_ID Staff_Name
36 1 员工甲
37 1 员工乙
38 1 员工丙
39 3 员工甲
40 3 员工乙
41 5 员工丙
42 5 员工甲
48 1 员工丁
50 5 员工乙

如何用一句sql判断员工甲和员工乙是否在同一部门?

...全文
327 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
银狐被占用 2003-11-06
  • 打赏
  • 举报
回复
select * from 表 where (select Dept_ID from 表 where staff_Name = '员工甲') as Dept_ID1 in (select Dept_ID from 表 where staff_Name = '员工乙') as Dept_ID2
realgz 2003-11-06
  • 打赏
  • 举报
回复
select case when exists
(select 1 from [table] t ,[table ] t2
where t.Staff_Name in ('员工甲','员工乙') and t2.Staff_Name in ('员工甲','员工乙') and t.dept_id=t2.dept_id and t.Staff_Name<>t2.Staff_Name ) then '在同一部门' else
'不在同一部门' end
今天怎么写得这么复杂?回去睡觉。。。。。。
pengdali 2003-11-06
  • 打赏
  • 举报
回复
select * from 表 where Staff_Name='员工甲' and dept_id in (select dept_id from 表 where Staff_Name='员工乙')
lvltt 2003-11-06
  • 打赏
  • 举报
回复
Select Dept_ID from 表 where staff_Name = '员工甲' and Dept_ID in (Select Dept_ID from 表 where staff_Name = '员工乙')
pengdali 2003-11-06
  • 打赏
  • 举报
回复
if exists (select 1 from 表 a,表 b where a.Staff_Name='员工甲' and b.Staff_Name='员工乙' and a.Dept_ID=b.Dept_ID)
select '在'
txlicenhe 2003-11-06
  • 打赏
  • 举报
回复
Select Dept_ID from 表 where staff_Name = '员工乙' and Dept_ID in (Select Dept_ID from 表 where staff_Name = '员工甲')
txlicenhe 2003-11-06
  • 打赏
  • 举报
回复
Select '是在同一部门' where exists
(Select Dept_ID from 表 where staff_Name = '员工乙' and Dept_ID in (Select Dept_ID from 表 where staff_Name = '员工甲') )
kinglccs 2003-11-06
  • 打赏
  • 举报
回复
haha .用一个内连接不就可以了吗?为什么大家写的都这么难呢?
zjcxc 元老 2003-11-06
  • 打赏
  • 举报
回复
select case a.dept_id when b.deptid then '同一部门' else '不同部门' end
from(select dept_id from 表 where staff_name='员工甲') a left join(select dept_id from 表 where staff_name='员工乙') b on a.dept_id=b.dept_id

34,875

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧