Mysql 创建连接以显示T1中不在T2中的一列的值,并进行一些where比较

Mysql 创建连接以显示T1中不在T2中的一列的值,并进行一些where比较,mysql,select,join,where,procedures,Mysql,Select,Join,Where,Procedures,我正在尝试在MYSQL存储过程中创建联接,一个表是EMployeeMaster,另一个表是WeekRest 对于第二个表WeekRest中不存在EMPID的EMPID,我的联接应该只为这些EMPID从EmpMaster生成EMPID值。我无法得到预期的结果 为表和我创建的联接附加表结构,其中只包含必要的列和示例值 为第一个表创建语句hrmempmaster CREATE TABLE `hrmempmaster` ( `ID` int(11) NOT NULL AUTO_INCREME

我正在尝试在MYSQL存储过程中创建联接,一个表是EMployeeMaster,另一个表是WeekRest

对于第二个表WeekRest中不存在EMPID的EMPID,我的联接应该只为这些EMPID从EmpMaster生成EMPID值。我无法得到预期的结果

为表和我创建的联接附加表结构,其中只包含必要的列和示例值

  • 为第一个表创建语句hrmempmaster

        CREATE TABLE `hrmempmaster` (
      `ID` int(11) NOT NULL AUTO_INCREMENT,
      `EMPID` varchar(45) NOT NULL,
      `Department` varchar(45) DEFAULT NULL,
      `Designation` varchar(45) DEFAULT NULL,
      `ShiftName` varchar(45) DEFAULT NULL,
      `ShiftID` varchar(45) DEFAULT NULL,
      `ReportingTo` varchar(45) DEFAULT NULL,
      `UnitCode` varchar(45) DEFAULT NULL,
      `UnitName` varchar(45) DEFAULT NULL,
      `DivisionName` varchar(45) DEFAULT NULL,
      `DivisionCode` varchar(45) DEFAULT NULL,
      `Name` varchar(50) DEFAULT NULL,
      `Gender` varchar(50) DEFAULT NULL,
      `DOB` varchar(50) DEFAULT NULL,
      `DOJ` varchar(50) DEFAULT NULL,
      `FatherName` varchar(45) DEFAULT NULL,
      PRIMARY KEY (`ID`),
      UNIQUE KEY `EMPID_UNIQUE` (`EMPID`),
      UNIQUE KEY `ID_UNIQUE` (`ID`)
    ) ENGINE=InnoDB AUTO_INCREMENT=428 DEFAULT CHARSET=utf8;
    
  • 为第二个表创建语句WeekRest

    CREATE TABLE `hrmweekrest` (
      `ID` int(11) DEFAULT '1',
      `EmpID` varchar(45) NOT NULL,
      `EmpName` varchar(45) DEFAULT NULL,
      `FatherName` varchar(45) DEFAULT NULL,
      `Department` varchar(45) DEFAULT NULL,
      `Designation` varchar(45) DEFAULT NULL,
      `WeekRestOn` varchar(45) DEFAULT NULL,
      `Month` varchar(45) NOT NULL,
      `Year` varchar(45) NOT NULL,
      `Status` varchar(45) DEFAULT NULL,
      `MonthYear` varchar(45) DEFAULT NULL,
      PRIMARY KEY (`EmpID`,`Month`,`Year`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    
  • 我的程序没有给出预期值

    CREATE DEFINER=`root`@`localhost` PROCEDURE `HRFetchWeekRest`(IN Department VARCHAR(25), IN MonthYear varchar (15))
    begin
    
    select hrmempmaster.empid, hrmempmaster.Name,hrmempmaster.FatherName, hrmempmaster.Department,hrmempmaster.Designation, HRMWEEKREST.WeekRestOn FROM HRMEMPMASTER
    
    LEFT JOIN HRMWEEKREST ON HRMEMPMASTER.EMPID = HRMWEEKREST.EMPID
    
    where hrmempmaster.empid not in (select empid from HRMWEEKREST where HRMWEEKREST.MonthYear = MonthYear  ) and  hrmempmaster.department = Department;
    
    end
    
  • 范例

  • 若EmployeeMaster表中有1名员工的EMPID为ID1、ID2、ID3….,并且我将部门作为Dept1传递给所有员工,则WeekRest表的列中包含以下值

        # ID, EmpID, EmpName, FatherName, Department, Designation, WeekRestOn, Month, Year, Status, MonthYear
        1, ID1, Name1, Fname1, Dept1, Designation1, Sunday, January, 2017, Active, January2017
    
    那么什么时候

    i) 部门通过为“部门1”,月通过为“2017年2月” select语句应该返回所有EMPID,因为它们在Weekrest表中都不具有值。将二月视为一年中除一月以外的任何其他月份

    ii)部门通过为“部门1”,月通过为“2017年1月” select语句应返回除ID1之外的所有EMPID,因为除ID1之外,所有ID在Weekrest表中都没有值

    将尝试最早提供任何进一步的信息

    试试这个

     CREATE DEFINER=`root`@`localhost` PROCEDURE `HRFetchWeekRest`(IN Department VARCHAR(25), IN MonthYear varchar (15))
        begin
    
        select hrmempmaster.empid, hrmempmaster.Name,hrmempmaster.FatherName, hrmempmaster.Department,hrmempmaster.Designation, HRMWEEKREST.WeekRestOn FROM HRMEMPMASTER
    
        LEFT JOIN HRMWEEKREST ON HRMEMPMASTER.EMPID = HRMWEEKREST.EMPID AND HRMWEEKREST.MonthYear = MonthYear
    
        where HRMWEEKREST.empid  IS NULL and  hrmempmaster.department = Department;
    
        end
    

    为了描述实际问题,你应该大幅减少所有代码和单词