来自第二个查询的未经考虑的空结果

时间:2015-11-28 11:22:39

标签: mysql

我有两个问题,我一个接一个地运行。

第一个查询给出了至少有一个列出的类型(LiTy计数> 0)的办事处的ID和名称:

SELECT Off, OffId, COUNT(*) AS NumLiTy
FROM LiTyT
INNER JOIN TaOff USING(OffId)
WHERE Upd Is Null
OR (Upd < DATE_ADD(NOW(), INTERVAL -10  HOUR))
AND (
    Def IS NULL
    OR Def NOT IN (2, 3))
GROUP BY OffId HAVING NumLiTy > 0 

第二个查询为我提供了每个列出类型的数据。

SELECT Id, RG, Rif_Year, Rif_Sec,
    IF(Dep_Date='0000-00-00' OR Dep_Date IS NULL,'',Dep_Date) AS Dep_Date,
    Rif_Type, Rif_Obj, Rif_DCU, Rif_DC, 
(
    SELECT Count(*)
    FROM `dt1`
    WHERE `IdLiTy` = LiTyT.Id) AS P_RT,
(
    SELECT Count(*)
    FROM `dt2`
    WHERE `IdLiTy` = LiTyT.Id) AS At_Dep,
(
    SELECT Count(*)
    FROM `dt3`
    WHERE `IdLiTy` = LiTyT.Id) AS Disc,
(
    SELECT Count(*)
    FROM `dt4`
    WHERE `IdLiTy` = LiTyT.Id) AS Provv_C,
(
    SELECT Count(*)
    FROM `dt5`
    WHERE `IdLiTy` = LiTyT.Id) AS Provv_M
FROM LiTyT
WHERE OffId = '1'
AND
(
    LiTyT.Upd IS Null
    OR
    (LiTyT.Upd < DATE_ADD(NOW(), INTERVAL -10  HOUR)))
AND
(
    LiTyT.Def IS NULL
    OR LiTyT.Def NOT IN (2, 3)) 

问题在于:

第一次查询给了我三个办公室,包括&gt; 0

Off      OffId   NumLiTy
Office_1    1      8
Office_2    2      2
Office_3    3      1

但是,当我尝试从第一个办公室获取数据(谁有数= 8)时,我没有得到任何行。

很明显,这两个查询显然只是使用相同的标准,但我无法找到差异并修复它们。

修改: 这是一个检查查询的模式,但我在sqlfiddle上尝试了它,一切似乎都有效。

CREATE TABLE IF NOT EXISTS `dt1` (
`IdEv` bigint(20) NOT NULL,
  `IdLiTy` bigint(20) NOT NULL,
  `Type` varchar(50) DEFAULT NULL,
  `Cost_Date` date DEFAULT NULL,
  `Rin_Date` date DEFAULT NULL,
  `Upd` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `Notice` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00'
) ENGINE=InnoDB AUTO_INCREMENT=103 DEFAULT CHARSET=utf8;

INSERT INTO `dt1` (`IdEv`, `IdLiTy`, `Type`, `Cost_Date`, `Rin_Date`, `Upd`, `Notice`) VALUES
(1, 1, 'Rico', '2014-01-02', NULL, '2015-11-27 09:54:55', '0000-00-00 00:00:00'),
(2, 1, 'Resist', '2014-01-17', NULL, '2015-11-27 09:54:55', '0000-00-00 00:00:00'),
(3, 2, 'Rico', '2015-01-02', NULL, '2015-11-27 09:55:15', '0000-00-00 00:00:00'),
(4, 2, 'Resist', NULL, NULL, '2015-11-27 09:55:15', '0000-00-00 00:00:00'),
(5, 3, 'Rico', '1999-01-02', NULL, '2015-11-27 09:55:33', '0000-00-00 00:00:00'),
(6, 3, 'Resist', '1999-01-07', NULL, '2015-11-27 09:55:33', '0000-00-00 00:00:00'),
(7, 10, 'Rico', '2015-01-14', NULL, '2015-11-27 09:55:58', '0000-00-00 00:00:00'),
(8, 10, 'Resist', NULL, NULL, '2015-11-27 09:55:58', '0000-00-00 00:00:00'),
(9, 50, 'Rico', '2015-02-10', NULL, '2015-11-27 09:56:18', '0000-00-00 00:00:00'),
(10, 50, 'Resist', '2015-02-12', NULL, '2015-11-27 09:56:18', '0000-00-00 00:00:00');

CREATE TABLE `dt2` (
`IdEv` bigint(20) NOT NULL,
 `IdLiTy` bigint(20) NOT NULL,
 `Att_Type` varchar(200) DEFAULT NULL,
  `Number` int(10) DEFAULT NULL,
  `Part` varchar(50) DEFAULT NULL,
  `Dep_Date` date DEFAULT NULL,
  `Upd` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

INSERT INTO `dt2` (`IdEv`, `IdLiTy`, `Att_Type`, `Number`, `Part`, `Dep_Date`, `Upd`) VALUES
(1, 1, 'MEMORY >', 2014006804, 'Rico', '2014-12-13', '2015-11-27 09:54:55'),
(2, 1, 'DEP > DOC', 2014006803, 'Rico', '2014-12-13', '2015-11-27 09:54:55'),
(3, 1, 'DEP > DOC', 2014006625, 'Rico', '2014-12-03', '2015-11-27 09:54:55'),
(4, 1, 'DEP > DOC', 2014003219, 'Rico', '2014-05-30', '2015-11-27 09:54:55'),
(5, 1, 'DEP > DOC', 2014000351, 'Resist', '2014-01-18', '2015-11-27 09:54:55'),
(6, 1, 'ATT > COST', 2014000325, 'Resist', '2014-01-17', '2015-11-27 09:54:55'),
(7, 1, 'DOM > FISS', 2014000001, 'Rico', '2014-01-02', '2015-11-27 09:54:55'),
(8, 1, 'RICO', 2014000010, 'Rico', '2014-01-02', '2015-11-27 09:54:55'),
(9, 2, 'DEP > DOC', 2015001574, 'Rico', '2015-04-08', '2015-11-27 09:55:16');

CREATE TABLE `dt3` (
`IdEv` bigint(20) NOT NULL,
  `IdLiTy` bigint(20) NOT NULL,
  `Udi_Date` date DEFAULT NULL,
  `Udi_Type` varchar(50) DEFAULT NULL,
  `Es_Disc` varchar(200) DEFAULT NULL,
  `Relat` varchar(50) DEFAULT NULL,
  `Group` varchar(50) DEFAULT NULL,
  `Ru_Ord` int(5) DEFAULT NULL,
  `Upd` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `dt3` (`IdEv`, `IdLiTy`, `Udi_Date`, `Udi_Type`, `Es_Disc`, `Relat`, `Group`, `Ru_Ord`, `Upd`) VALUES
(1, 1, '2016-05-11', 'UDI PUB', '', 'MAGI', 'Meri', 0, '2015-11-27 09:54:55'),
(2, 1, '2014-12-17', 'CAM_CON', 'DISC_O', 'ABBR', 'Giu_Ca', 12, '2015-11-27 09:54:55'),
(3, 1, '2014-06-04', 'CAM_CON', 'DISC_O', 'ABBR', 'Giu_Ca', 8, '2015-11-27 09:54:55');

CREATE TABLE `dt4` (`IdEv` bigint(20) NOT NULL,
  `IdLiTy` bigint(20) NOT NULL,
  `Type` varchar(50) DEFAULT NULL,
  `Number` int(10) DEFAULT NULL,
  `Relat` varchar(50) DEFAULT NULL,
  `Pub_Date` date DEFAULT NULL,
  `Udi_Date` date DEFAULT NULL,
  `Udi_Type` varchar(50) DEFAULT NULL,
  `Outcome` varchar(50) DEFAULT NULL,
  `Upd` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `dt4` (`IdEv`, `IdLiTy`, `Type`, `Number`, `Relat`, `Pub_Date`, `Udi_Date`, `Udi_Type`, `Outcome`, `Upd`) VALUES
(1, 1, 'ORD_CAUT', 201400396, 'ABBR', '2014-12-13', '2014-12-17', 'CC', 'ACC', '2015-11-27 09:54:55'),
(2, 1, 'ORD_CAUT', 201400165, 'ABBR', '2014-12-13', '2014-06-04', 'CC', 'FISS', '2015-11-27 09:54:55'),
(3, 1, 'ORD_CAUT', 201400023, 'ABBR', '2014-12-03', '2014-01-22', 'CC', 'FISS', '2015-11-27 09:54:55'),
(4, 2, 'SEN', 201500639, 'PASS', '2015-04-08', '2015-09-09', 'CC', 'IMPROCEDIBILE PER SOPRAVVENUTA CARENZA DI INTERESS', '2015-11-27 09:55:16'),
(5, 3, 'SEN', 200000894, 'SPEC', '1999-02-16', '2000-06-07', 'UP', 'IMPR', '2015-11-27 09:55:33'),
(6, 3, 'ORD_CAUT', 199900008, 'SPEC', '1999-01-07', '1999-01-13', 'CC', 'S_ACC', '2015-11-27 09:55:33');

CREATE TABLE `dt5` (
`IdEv` bigint(20) NOT NULL,
 `IdLiTy` bigint(20) NOT NULL,
 `Type` varchar(50) DEFAULT NULL,
 `Number` int(10) DEFAULT NULL,
 `Relat` varchar(50) DEFAULT NULL,
  `Pub_Date` date DEFAULT NULL,
  `Outcome` varchar(50) DEFAULT NULL,
  `Upd` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `dt5` (`IdEv`,  `IdLiTy`, `Type`, `Number`,  `Relat`, `Pub_Date`, `Outcome`, `Upd`) VALUES
(1, 247, 'GP', 201500007, 'PASS', '2015-07-15', 'RES', '2015-11-27 09:57:44'),
(2, 258, 'GP', 201500008, 'PASS', '2015-07-15', 'ACC', '2015-11-27 09:58:12'),
(3, 287, 'DD', 201500018, 'MOLL', '2015-09-19', 'DE', '2015-11-27 09:59:07'),
(4, 395, 'DC', 201500143, 'MOLL', '2015-07-30', 'ACC', '2015-11-27 10:00:36'),
(5, 454, 'DC', 201500161, 'PASS', '2015-09-04', 'ACC', '2015-11-27 10:00:58'),
(6, 556, 'DP', 201500133, 'RIGG', '2015-06-10', 'ACCO', '2015-11-28 09:25:40'),
(7, 230, 'DC', 201500103, 'POLI', '2015-04-20', 'RES', '2015-11-28 09:29:01'),
(8, 230, 'DC', 201500090, 'POLI', '2015-04-02', 'ACC', '2015-11-28 09:29:01'),
(9, 771, 'DC', 201500259, 'POLI', '2015-10-14', 'RES', '2015-11-28 09:32:35');

CREATE TABLE `LiTyT` (
`Id` bigint(20) NOT NULL,
  `Reg` char(30) NOT NULL,
  `OffId` smallint(4) NOT NULL,
 `RG` int(11) NOT NULL,
  `Rif_Year` mediumint(4) NOT NULL,
  `Sub` tinyint(2) DEFAULT NULL,
  `Rif_Sec` tinyint(2) NOT NULL,
  `Dep_Date` date NOT NULL,
  `Rif_Type` varchar(50) NOT NULL,
  `Rif_Obj` varchar(200) NOT NULL,
  `Rif_DCU` char(1) DEFAULT NULL,
  `Rif_DC` char(1) DEFAULT NULL,
  `Def` tinyint(2) DEFAULT NULL,
  `Def_Date` date DEFAULT NULL,
  `Upd` timestamp NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB AUTO_INCREMENT=1008 DEFAULT CHARSET=utf8;

INSERT INTO `LiTyT` (`Id`,`Reg`,`OffId`, `RG`, `Rif_Year`, `Sub`, `Rif_Sec`, `Dep_Date`, `Rif_Type`, `Rif_Obj`, `Rif_DCU`, `Rif_DC`, `Def`, `Def_Date`, `Upd`) VALUES
(1, '1', 1, 1, 2014, 1, 1, '2014-01-02', 'ORD', 'xxxx', NULL, 'S', NULL, NULL, '2015-11-28 09:19:40'),
(2, '1', 1, 2, 2015, 2, 1, '2015-01-02', 'ORD', 'xxxx', NULL, 'N', 0, NULL, '2015-11-28 09:19:48'),
(3, '1', 1, 1, 1999, 1, 1, '1999-01-02', 'ORD', 'xxxx', NULL, 'S', 0, NULL, '2015-11-28 09:19:56'),
(4, '15', 27, 432, 2015, 0, 2, '2015-01-28', 'ORD', 'xxxx', 'S', 'S', 0, NULL, NULL);

CREATE TABLE `TaOff` (
  `OffId` smallint(4) NOT NULL,
  `RegId` tinyint(2) NOT NULL,
  `Off` char(200) NOT NULL,
  `WId` varchar(30) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `TaOff` (`OffId`, `RegId`, `Off`, `WId`) VALUES
(1, 13, 'Office1', 'xxxx'),
(2, 17, 'Office2', 'xxxx'),
(3, 18, 'Office3', 'xxxx'),
(4, 15, 'Office4', 'xxxx'),
(5, 15, 'Office5', 'xxxx'),
(6, 12, 'Office6', 'xxxx'),
(7, 7, 'Office7', 'xxxx'),
(8, 3, 'Office8', 'xxxx');

1 个答案:

答案 0 :(得分:0)

最后我发现了这个错误!

这是因为在WHERE子句中使用了不正确的括号。所以这就是我现在所拥有的(我故意格式化):

SELECT OffId, COUNT(*) AS NumLiTy
FROM LiTyT
WHERE Upd Is Null
    OR (
           (
           Upd < DATE_ADD(NOW(), INTERVAL -10  HOUR)
           )
       AND (
           Def IS NULL
           OR (
              Def NOT IN (2, 3)
              )
           )
        )
GROUP BY OffId HAVING NumLiTy > 0

SELECT Id, RG, Rif_Year, Rif_Sec,
    IF(Dep_Date='0000-00-00' OR Dep_Date IS NULL,'',Dep_Date) AS Dep_Date,
    Rif_Type, Rif_Obj, Rif_DCU, Rif_DC,
   (SELECT Count(*)
    FROM `dt1`
    WHERE `IdLiTy` = LiTyT.Id) AS P_RT,
   (SELECT Count(*)
    FROM `dt2`
    WHERE `IdLiTy` = LiTyT.Id) AS At_Dep,
   (SELECT Count(*)
    FROM `dt3`
    WHERE `IdLiTy` = LiTyT.Id) AS Disc,
   (SELECT Count(*)
    FROM `dt4`
    WHERE `IdLiTy` = LiTyT.Id) AS Provv_C,
   (SELECT Count(*)
    FROM `dt5`
    WHERE `IdLiTy` = LiTyT.Id) AS Provv_M
FROM LiTyT
WHERE OffId = '1'
    AND (
        Upd Is Null
        OR (
               (
               Upd < DATE_ADD(NOW(), INTERVAL -10  HOUR)
               )
           AND (
               Def IS NULL
               OR (
                  Def NOT IN (2, 3)
                  )
               )
           )
         )