如何通过部门表查询出一个员工所在的部门、科室到班组,求SQL如何写?

cnzz84 2013-07-27 02:53:24
现在有两张表:部门表,员工信息表

表结构如下:
部门表


员工表


其中,departType分为三个值:1,2,3
1 = 公司名称(这个无需显示)
2 = 部门
3 = 科室
4 = 班组

通过这两张表的关联关系,想要实现的最终结果集为:



请问,这样的sql查询语句要如何写?
...全文
1324 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
栗华山 2013-07-30
  • 打赏
  • 举报
回复
对,你先把建表语句,插于语句都写好了。环境搭建起来了才好写sql啊!!!
ACMAIN_CHM 2013-07-30
  • 打赏
  • 举报
回复
引用 1 楼 ACMAIN_CHM 的回复:
建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。 参考一下这个贴子的提问方式http://bbs.csdn.net/topics/320211382 1. 你的 create table xxx .. 语句 2. 你的 insert into xxx ... 语句 3. 结果是什么样,(并给以简单的算法描述) 4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL) 这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。
因为你不愿意花时间帮准备答题者准备这些数据。而答题者则看时间心情什么时候空才会自己准备这些数据。
cnzz84 2013-07-30
  • 打赏
  • 举报
回复
引用 6 楼 cnido 的回复:
我工作中也遇到类似的问题,求大神解答。
主要是现在没有人能够帮助解答了,我自己测试了,也做了很多,还是没有搞出来。
cnido 2013-07-30
  • 打赏
  • 举报
回复
我工作中也遇到类似的问题,求大神解答。
cnzz84 2013-07-29
  • 打赏
  • 举报
回复
求高人帮忙解答一下。
cnzz84 2013-07-27
  • 打赏
  • 举报
回复
引用 2 楼 zhangle910522 的回复:
select
ifnull((select departName from 部门表 where departID = a.deparID and departType = 1),'无') as 部门,
ifnull((select departName from 部门表 where departID = a.deparID and departType = 1),'无') as 科室,
ifnull((select departName from 部门表 where departID = a.deparID and departType = 1),'无') as 班组,
username
from 员工表 as a group by userID


我三楼给你的回复,您可以可以忽略了。最后修改了一下sql语句,查询出来了,只是感觉好像不是太完整,没有上级部门的信息,都是单独的。


select
ifnull((select departName from oa_department where departID = userinfo.departID and departType = 1),'无') as bm,
ifnull((select departName from oa_department where departID = userinfo.departID and departType = 2),'无') as ks,
ifnull((select departName from oa_department where departID = userinfo.departID and departType = 3),'无') as bz,
userName,
realName
from oa_sys_userpost as userinfo group by userinfo.userID



结果:
cnzz84 2013-07-27
  • 打赏
  • 举报
回复
引用 2 楼 zhangle910522 的回复:
select ifnull((select departName from 部门表 where departID = a.deparID and departType = 1),'无') as 部门, ifnull((select departName from 部门表 where departID = a.deparID and departType = 1),'无') as 科室, ifnull((select departName from 部门表 where departID = a.deparID and departType = 1),'无') as 班组, username from 员工表 as a group by userID

select 
    if null((select departName from oa_department  where departID = userinfo.deparID and departType = 1),'无') as bm,
    if null((select departName from oa_department  where departID = userinfo.deparID and departType = 1),'无') as ks,
    if null((select departName from oa_department  where departID = userinfo.deparID and departType = 1),'无') as bz,
    userName
   from oa_sys_userpost as userinfo group by userinfo.userID
我是按照博主的这样写的,可是数据库提示的错误信息如下:

[SQL] select 
    if null((select departName from oa_department  where departID = userinfo.deparID and departType = 1),'无') as bm,
    if null((select departName from oa_department  where departID = userinfo.deparID and departType = 1),'无') as ks,
    if null((select departName from oa_department  where departID = userinfo.deparID and departType = 1),'无') as bz,
    userName
   from oa_sys_userpost as userinfo group by userinfo.userID

[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'null((select departName from oa_department  where departID = userinfo.deparID an' at line 2
树根之泥 2013-07-27
  • 打赏
  • 举报
回复
select ifnull((select departName from 部门表 where departID = a.deparID and departType = 1),'无') as 部门, ifnull((select departName from 部门表 where departID = a.deparID and departType = 1),'无') as 科室, ifnull((select departName from 部门表 where departID = a.deparID and departType = 1),'无') as 班组, username from 员工表 as a group by userID
ACMAIN_CHM 2013-07-27
  • 打赏
  • 举报
回复
建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。 参考一下这个贴子的提问方式http://bbs.csdn.net/topics/320211382 1. 你的 create table xxx .. 语句 2. 你的 insert into xxx ... 语句 3. 结果是什么样,(并给以简单的算法描述) 4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL) 这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。

56,677

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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