复杂的mysql查询以获取特定用户的组织层次结构

时间:2018-10-30 16:50:53

标签: mysql sql mybatis

我有3张桌子

用户,
  org_unit用于组织单位,
  org_unit_type用于组织单位类型

1.user表具有外键org_unit_id,它引用org_unit表

2.org_unit表的上级组织单位为外键(父级)

需要生成结果以获取特定用户的所有父级组织单位层次结构。

示例:员工13 组织-> TEAM3-UNIT4-SECTION1-DEPARTMENT2-ORGANIZATION1

我的组织层次结构 enter image description here

我正在尝试获得类似的东西。但是它需要达到最高水平

    SELECT 
    u.id,
    if(ou.id != parent,
        concat(ou.name,
                '-',
              (SELECT 
                        name
                    FROM
                        org_unit
                    where
                        id = ou.parent)),
        ou.name) as organization
FROM
    user u
        inner join
    org_unit ou ON u.org_unit = ou.id;

我的用户表

enter image description here

org_unit表

enter image description here

org_unit_type

enter image description here

请有人可以帮助获得结果表(作为第二张图片)。谢谢 附加的模式

   CREATE DATABASE  IF NOT EXISTS `final` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `final`;
-- MySQL dump 10.13  Distrib 5.6.13, for Win32 (x86)
--
-- Host: 127.0.0.1    Database: final
-- ------------------------------------------------------
-- Server version   5.7.17-log

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `org_unit`
--

DROP TABLE IF EXISTS `org_unit`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `org_unit` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `type` bigint(20) NOT NULL,
  `code` varchar(45) DEFAULT NULL,
  `name` varchar(45) DEFAULT NULL,
  `led_by` bigint(20) DEFAULT NULL,
  `parent` bigint(20) NOT NULL,
  `order_rank` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_org_unit_org_unit1_idx` (`parent`),
  KEY `fk_org_unit_org_unit_type1` (`type`),
  KEY `fk_org_unit_user1_idx` (`led_by`),
  CONSTRAINT `fk_org_unit_led_by_user_id` FOREIGN KEY (`led_by`) REFERENCES `user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `fk_org_unit_org_unit_org_unit_id` FOREIGN KEY (`parent`) REFERENCES `org_unit` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `fk_org_unit_type_org_unit_type_id` FOREIGN KEY (`type`) REFERENCES `org_unit_type` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `org_unit`
--

LOCK TABLES `org_unit` WRITE;
/*!40000 ALTER TABLE `org_unit` DISABLE KEYS */;
INSERT INTO `org_unit` VALUES (1,1,'ORG','Organization1',1,1,1),(2,2,'DEP1','DEPARTMENT1',2,1,2),(3,2,'DEP2','DEPARTMENT2',3,1,3),(4,2,'DEP3','DEPARTMENT3',5,1,3),(5,3,'SEC1','SECTION1',2,3,4),(6,3,'SEC2','SECTION2',2,4,4),(7,4,'UNT1','UNIT1',3,2,5),(8,4,'UNT2','UNIT2',NULL,1,1),(9,3,'SEC3','SECTION3',9,3,1),(10,5,'TEM2','TEAM2',4,7,6),(11,5,'TEM1','TEAM1',NULL,7,6),(12,4,'UNIT4','UNIT4',1,5,4),(13,5,'TEM3','TAEM3',1,12,5);
/*!40000 ALTER TABLE `org_unit` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Table structure for table `user`
--

DROP TABLE IF EXISTS `user`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `user` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `first_name` varchar(45) DEFAULT NULL,
  `middle_name` varchar(45) DEFAULT NULL,
  `last_name` varchar(45) DEFAULT NULL,
  `date_of_birth` date DEFAULT NULL,
  `mobile` varchar(45) DEFAULT NULL,
  `email` varchar(45) DEFAULT NULL,
  `blood_group` varchar(45) DEFAULT NULL,
  `profile_image` varchar(255) DEFAULT NULL,
  `date_of_join` date DEFAULT NULL,
  `designation` varchar(45) DEFAULT NULL,
  `work_location` varchar(45) DEFAULT NULL,
  `reporting_manager` bigint(20) NOT NULL,
  `org_unit` bigint(20) NOT NULL,
  `personal_note` varchar(255) DEFAULT NULL,
  `feeling` bigint(20) NOT NULL,
  `order_rank` int(11) DEFAULT NULL,
  `active` bit(1) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_user_feeling1` (`feeling`),
  KEY `fk_user_org_unit1` (`org_unit`),
  KEY `fk_user_user` (`reporting_manager`),
  CONSTRAINT `fk_user_feeling_feeling_id` FOREIGN KEY (`feeling`) REFERENCES `feeling` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `fk_user_org_unit_org_unit_id` FOREIGN KEY (`org_unit`) REFERENCES `org_unit` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `fk_user_reporting_manager_user_id` FOREIGN KEY (`reporting_manager`) REFERENCES `user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `user`
--

LOCK TABLES `user` WRITE;
/*!40000 ALTER TABLE `user` DISABLE KEYS */;
INSERT INTO `user` VALUES (1,'Mohamed','Musni',NULL,'1989-12-17','0716351812','musni@gmail.com','O+','https://www.google.lk/seafdgfgfg','2010-02-10','CO','Colombo',1,1,'test1',1,1,''),(2,'kumuditha','tharanga',NULL,'1989-12-17','0716351812','kumuditha@gmail.lk','A+','https://www.google.lk/seafdgfgfg','2010-02-10','Engineer','Colombo',1,2,'test2',1,2,''),(3,'Yasas','ravin','Karunarathna','1985-12-22','0716351812','yasas@gmail.com','O+','https://www.google.lk/seafdgfgfg','2010-02-10','Consultant','Colombo',1,3,'test3',1,2,''),(4,'tharanga','hhhh','wew','1985-12-22','0766351812','tharanga@gmail.com','AB','https://www.google.lk/seafdgfgfg','2010-02-10','Senoir Engineer','Colombo',3,2,'test4',2,3,''),(5,'damith','asanka','eefe','1985-12-22','0766351111','damith@gmail.com','B+','https://www.google.lk/seafdgfgfg','2010-02-10','Engineer','Colombo',4,4,'test5',3,1,''),(6,'kasun','eee','ddd','1985-12-22','0766351111','kasun@gmail.com','','https://www.google.lk/seafdgfgfg','2011-03-10','Associate Enginner','Colombo',1,1,'test5',1,1,''),(7,'nimal','ewwre','fedfe','1985-12-22','0766351122','nimal@gmail.com','A+','https://www.google.lk/seafdgfgfg','2011-03-10','Senoir Engineer','Colombo',2,10,'test6',4,2,''),(8,'yasas','ravin',NULL,'1985-12-22','0766351122','ayasas@gmail.com','O+','https://www.google.lk/seafdgfgfg','2011-03-10','Senoir Engineer','Colombo',1,5,'test7',1,1,''),(9,'','ravin',NULL,'1985-12-22','0766351122','ayasas@gmail.com','A+','https://www.google.lk/seafdgfgfg','2011-03-10','Engineer','Colombo',2,9,'test8',1,1,''),(10,'kevin','perera',NULL,'1985-12-20','0766351111','kevins@gmail.com','O+','https://www.google.lk','2015-03-10','QA Enginner','CMB',2,9,'test9',1,2,''),(11,'namal','rupasinghe',NULL,'1985-12-20','0716351111','namal@gmail.com','A+','https://www.google.lk','2015-04-10','UI Engineer','ATC',3,3,'test10',1,1,''),(12,'ravin','pp','mm','1985-12-20','0716351111','ravin@gmail.com','O-','https://www.google.lk/yyyyyyyyyy','2014-04-10','QA Enginner','ATC',3,12,'test11',1,1,''),(13,'gayan','sampath','kk','1985-12-20','0713333333','gayan@gmail.com','B+','https://www.google.lk/yyyyyyyyyy','2014-04-18','Engineer','CMB',3,13,'test12',1,1,'');
/*!40000 ALTER TABLE `user` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Table structure for table `org_unit_type`
--

DROP TABLE IF EXISTS `org_unit_type`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `org_unit_type` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `display_name` varchar(255) DEFAULT NULL,
  `plural_name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name_UNIQUE` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `org_unit_type`
--

LOCK TABLES `org_unit_type` WRITE;
/*!40000 ALTER TABLE `org_unit_type` DISABLE KEYS */;
INSERT INTO `org_unit_type` VALUES (1,'organization','Organization','Organizations'),(2,'department','Department','Departments'),(3,'section','Section','Sections'),(4,'unit','Unit','Units'),(5,'team','Team','Teams');
/*!40000 ALTER TABLE `org_unit_type` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Table structure for table `feeling`
--

DROP TABLE IF EXISTS `feeling`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `feeling` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(45) DEFAULT NULL,
  `display_name` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name_UNIQUE` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `feeling`
--

LOCK TABLES `feeling` WRITE;
/*!40000 ALTER TABLE `feeling` DISABLE KEYS */;
INSERT INTO `feeling` VALUES (1,'happy','Happy'),(2,'relaxed','Relaxed'),(3,'excited','Excited'),(4,'sad','Sad'),(5,'angry','Angry'),(6,'tired','Tired');
/*!40000 ALTER TABLE `feeling` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2018-10-31 12:26:06

0 个答案:

没有答案