首页 > 数据库 >SQL 获取当前部门、部门人员,以及它的子级

SQL 获取当前部门、部门人员,以及它的子级

时间:2023-09-15 17:36:18浏览次数:49  
标签:sjjgbh SQL jgbm dept 部门 子级 jgbh isdel SELECT

部门及子部门

(
	WITH RECURSIVE dept AS (
			SELECT id, jgjc as name, jgbm as jgbh, sjjgbh, 'JG' as type FROM t_jgxx
			WHERE jgbm = '111111' and isdel = 0
			UNION ALL
			SELECT d.id, d.jgjc as name, d.jgbm as jgbh, d.sjjgbh, 'JG' as type FROM t_jgxx d
			JOIN dept ON d.sjjgbh = dept.jgbh WHERE isdel = 0
	)
	SELECT * FROM dept
)

部门下的人员

(
	WITH RECURSIVE dept AS (
		SELECT id, jgjc as name, jgbm as jgbh, sjjgbh, 'JG' as type FROM t_jgxx
		WHERE jgbm = '111111' and isdel = 0
		UNION ALL
		SELECT d.id, d.jgjc as name, d.jgbm as jgbh, d.sjjgbh, 'JG' as type FROM t_jgxx d
		JOIN dept ON d.sjjgbh = dept.jgbh WHERE isdel = 0
	)
	SELECT bh as id,yhmc as name,dwbh as jgbh,'' as sjjgbh,'RY' as type FROM dept
	JOIN t_yhxx ON dept.jgbh = t_yhxx.dwbh WHERE isdel = 0  
)

合并部门和人员

(
	WITH RECURSIVE dept AS (
			SELECT id, jgjc as name, jgbm as jgbh, sjjgbh, 'JG' as type FROM t_jgxx
			WHERE jgbm = '111111' and isdel = 0
			UNION ALL
			SELECT d.id, d.jgjc as name, d.jgbm as jgbh, d.sjjgbh, 'JG' as type FROM t_jgxx d
			JOIN dept ON d.sjjgbh = dept.jgbh WHERE isdel = 0
	)
	SELECT * FROM dept
)

UNION ALL

(
	WITH RECURSIVE dept AS (
		SELECT id, jgjc as name, jgbm as jgbh, sjjgbh, 'JG' as type FROM t_jgxx
		WHERE jgbm = '111111' and isdel = 0
		UNION ALL
		SELECT d.id, d.jgjc as name, d.jgbm as jgbh, d.sjjgbh, 'JG' as type FROM t_jgxx d
		JOIN dept ON d.sjjgbh = dept.jgbh WHERE isdel = 0
	)
	SELECT bh as id,yhmc as name,dwbh as jgbh,'' as sjjgbh,'RY' as type FROM dept
	JOIN t_yhxx ON dept.jgbh = t_yhxx.dwbh WHERE isdel = 0  
)

标签:sjjgbh,SQL,jgbm,dept,部门,子级,jgbh,isdel,SELECT
From: https://www.cnblogs.com/lbx6935/p/17705554.html

相关文章

  • MySQL实战实战系列 01 基础架构:一条SQL查询语句是如何执行的?
    这是专栏的第一篇文章,我想来跟你聊聊MySQL的基础架构。我们经常说,看一个事儿千万不要直接陷入细节里,你应该先鸟瞰其全貌,这样能够帮助你从高维度理解问题。同样,对于MySQL的学习也是这样。平时我们使用数据库,看到的通常都是一个整体。比如,你有个最简单的表,表里只有一个ID......
  • CentOS 7.6安装MySQL8
    下载yum源yuminstall-ywgetwgethttps://dev.mysql.com/get/mysql80-community-release-el7-5.noarch.rpm安装yum源rpm-ivhmysql80-community-release-el7-5.noarch.rpm安装MySQLyuminstall-ymysql-server启动mysql服务systemctlstartmysqld查看......
  • MySQL实战实战系列 00 开篇词 这一次,让我们一起来搞懂MySQL
    你好,我是林晓斌,网名“丁奇”,欢迎加入我的专栏,和我一起开始MySQL学习之旅。我曾先后在百度和阿里任职,从事MySQL数据库方面的工作,一步步地从一个数据库小白成为MySQL内核开发人员。回想起来,从我第一次带着疑问翻MySQL的源码查到答案至今,已经有十个年头了。在这个过程中,......
  • MySQL篇:第四章_详解DML语言
    DML语言插入一、方式一语法:insertinto表名(字段名,...)values(值1,...);特点:1、字段类型和值类型一致或兼容,而且一一对应2、可以为空的字段,可以不用插入值,或用null填充3、不可以为空的字段,必须插入值4、字段个数和值的个数必须一致5、字段可以省略,但默认所有字段,并且......
  • PostgreqSQL 解压后
    如果不能运行installer,只能下载zip包假设解压到C盘,解压后:C:\pgsql\bin>initdb.exe-DC:\pgsql_data-Upostgres-W-EUTF8-Ascram-sha-256属于此数据库系统的文件宿主为用户"ausername".此用户也必须为服务器进程的宿主.数据库簇将使用本地化语言"Chinese(Simplifie......
  • Navicat连接Mysql数据显示2059 - authentication plugin ‘caching_sha2_password‘的
    安装Mysql8.0,使用navicat登录时显示如下错误提示 错误原因:MySQL新版本(8以上版本)的用户登录账户加密方式是【caching_sha2_password】,Navicat不支持这种用户登录账户加密方式。解决办法:1.打开MySQL命令行客户端 2.输入登录密码登录,查看加密方式,命令:showvariableslike'd......
  • 申通ubuntu1804快速装postgresql
    先eridocker一套初始化sudosh-c'echo"debhttp://apt.postgresql.org/pub/repos/apt$(lsb_release-cs)-pgdgmain">/etc/apt/sources.list.d/pgdg.list'wget--quiet-O-https://www.postgresql.org/media/keys/ACCC4CF8.asc|sudoapt-keyad......
  • .net链接mysql报错:给定关键字不在字典中,解决方案
    如果看到这个报错信息,大概率是的原因是:1、连接字符串中编码和数据库实际编码不一致。解决方案:修改连接字符串中的编码即可,保证和mysql中编码一致。 2、MySql.Data.dll版本不一致。解决方案:去mysql官网下载对应版本的dll;或使用VS的Nuget更新MySql.Data.dll。......
  • MySQL存储过程、索引、分表对比
    MySQL存储过程、索引和分表是用于提高查询效率的三种不同方法,它们各自对查询效率有不同的影响和应用场景。以下是它们的对比:MySQL存储过程:影响查询效率:存储过程通常不直接影响查询效率,因为它们是用于封装查询逻辑和执行多个SQL语句的数据库对象。存储过程主要有助于减少网络......
  • mysql8.0版本的下载与安装
    1.首先下载mysql,官网下载https://www.mysql.com/ 2.到页面底端,选择社区版 3.选择windows版本 4.选择较大的那个下载链接 5.跳过注册,直接下载二、Mysql8.0的安装1.双击下载后的安装文件,点击no,即自动升级选项  2.选择下一步  3. ......