每行具有动态用户定义变量的子查询

问题描述 投票:0回答:1

奇怪的问题,这可能根本不可能。 它围绕以下观点展开。如第二个视图 (ambulances_available) 所示,我试图根据第一个视图 (ambulances_shift_available) 计算可用的救护车数量。

以我的全部智慧(作为一个完全的新手,请阅读)我正在使用 2 个用户定义的变量。日期很简单,但需要 @shift_id 来获取每个班次的具体计数。

底部是我想看到的返回样本。

CREATE 
    ALGORITHM = UNDEFINED 
    DEFINER = `root`@`localhost` 
    SQL SECURITY DEFINER
VIEW `ambulances_shift_available` AS
    SELECT 
        `shifts_options`.`id` AS `id`,
        `shifts_options`.`timeStart` AS `timeStart`,
        `shifts_options`.`timeEnd` AS `timeEnd`,
        `shifts_options`.`locations_id` AS `locations_id`,
        `shifts_options`.`types_id` AS `types_id`,
        `ambulances`.`id` AS `ambulances_id`
    FROM
        (`shifts_options`
        JOIN `ambulances`)
    WHERE
        ((`shifts_options`.`id` = SHIFT_ID())
            AND (`ambulances`.`types_id` = `shifts_options`.`types_id`)
            AND (`ambulances`.`locations_id` = `shifts_options`.`locations_id`)
            AND `ambulances`.`id` IN (SELECT 
                `ambulances_unavailable`.`ambulances_id`
            FROM
                `ambulances_unavailable`
            WHERE
                ((TIMESTAMP(SPECIFICDATE(),
                    `shifts_options`.`timeStart`) BETWEEN CAST(`ambulances_unavailable`.`dateStart` AS DATETIME (6)) AND CAST(`ambulances_unavailable`.`dateEnd` AS DATETIME (6)))
                    OR (TIMESTAMP(SPECIFICDATE(),
                    `shifts_options`.`timeEnd`) BETWEEN CAST(`ambulances_unavailable`.`dateStart` AS DATETIME (6)) AND CAST(`ambulances_unavailable`.`dateEnd` AS DATETIME (6)))
                    OR (CAST(`ambulances_unavailable`.`dateStart` AS DATETIME (6)) BETWEEN TIMESTAMP(SPECIFICDATE(),
                    `shifts_options`.`timeStart`) AND TIMESTAMP(SPECIFICDATE(),
                    `shifts_options`.`timeEnd`))
                    OR (CAST(`ambulances_unavailable`.`dateEnd` AS DATETIME (6)) BETWEEN TIMESTAMP(SPECIFICDATE(),
                    `shifts_options`.`timeStart`) AND TIMESTAMP(SPECIFICDATE(),
                    `shifts_options`.`timeEnd`))))
            IS FALSE);

CREATE 
    ALGORITHM = UNDEFINED 
    DEFINER = `root`@`localhost` 
    SQL SECURITY DEFINER
VIEW `ambulances_available count` AS
    SELECT 
        `shifts_uncoupled`.`id` AS `id`,
        `shifts_uncoupled`.`date` AS `date`,
        `shifts_uncoupled`.`shifts_options_id` AS `shifts_options_id`,
        (SELECT count(*) from (select @shift_id:=`shift_options_id` p) parm, ambulances_shift_available)
    FROM
        `shifts_uncoupled`

| 73 | 73 2023-11-07 | D00 | 4 |
| 74 | 74 2023-11-07 | D10 | 6 |
| 76 | 76 2023-11-07 | D20 | 5 |
| 78 | 78 2023-11-07 | D30 | 3 |
| 79 | 79 2023-11-07 | D31 | 2 |

==========================================
请求编辑:

ambulances.id IN (...) IS FALSE
变体的原因很简单。我把它写成
ambulances.id NOT IN (...)
,Mysql-Workbench 改变了它。
CAST (...) AS DATETIME (6)
也是如此。我把它写成
TIMESTAMP(...)

希望这能提供更多澄清。

CREATE TABLE `ambulances` (
  `id` int NOT NULL,
  `licenseplate` varchar(10) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `types_id` int NOT NULL,
  `locations_id` int NOT NULL,
  `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `id` (`id`),
  KEY `types_id` (`types_id`),
  KEY `locations_id` (`locations_id`),
  CONSTRAINT `ambulances_ibfk_1` FOREIGN KEY (`types_id`) REFERENCES `types` (`id`),
  CONSTRAINT `ambulances_ibfk_2` FOREIGN KEY (`locations_id`) REFERENCES `locations` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

并且

CREATE TABLE `shifts_options` (
  `id` varchar(15) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `timeStart` time NOT NULL,
  `timeEnd` time NOT NULL,
  `locations_id` int NOT NULL,
  `types_id` int NOT NULL,
  `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
CREATE 
    ALGORITHM = UNDEFINED 
    DEFINER = `root`@`localhost` 
    SQL SECURITY DEFINER
VIEW `ambulances_unavailable` AS
    SELECT 
        `shifts_lists`.`id` AS `id`,
        `shifts_lists`.`ambulances_id` AS `ambulances_id`,
        CONCAT(`shifts_lists`.`date`,
                ' ',
                `shifts_options`.`timeStart`) AS `dateStart`,
        CONCAT((`shifts_lists`.`date` + INTERVAL 1 DAY),
                ' ',
                `shifts_options`.`timeEnd`) AS `dateEnd`,
        `shifts_lists`.`shifts_options_id` AS `reason`,
        `shifts_options`.`locations_id` AS `locations_id`,
        `shifts_options`.`types_id` AS `types_id`
    FROM
        (`shifts_lists`
        LEFT JOIN `shifts_options` ON ((`shifts_lists`.`shifts_options_id` = `shifts_options`.`id`)))
    WHERE
        (((`shifts_lists`.`date` = (SPECIFICDATE() - INTERVAL 1 DAY))
            AND (`shifts_options`.`timeStart` > '21:00'))
            OR ((`shifts_lists`.`date` = SPECIFICDATE())
            AND (`shifts_lists`.`ambulances_id` IS NOT NULL))) 
    UNION SELECT 
        `maintenances`.`id` AS `id`,
        `maintenances`.`ambulances_id` AS `ambulances_id`,
        `maintenances`.`dateStart` AS `dateStart`,
        `maintenances`.`dateEnd` AS `dateEnd`,
        'maintenance' AS `reason`,
        `ambulances`.`locations_id` AS `locations_id`,
        `ambulances`.`types_id` AS `types_id`
    FROM
        (`maintenances`
        JOIN `ambulances` ON ((`maintenances`.`ambulances_id` = `ambulances`.`id`)))
    WHERE
        ((`maintenances`.`dateStart` BETWEEN SPECIFICDATE() AND (SPECIFICDATE() + INTERVAL 1 DAY))
            OR (`maintenances`.`dateEnd` BETWEEN SPECIFICDATE() AND (SPECIFICDATE() + INTERVAL 1 DAY))
            OR ((`maintenances`.`dateStart` < SPECIFICDATE())
            AND (`maintenances`.`dateEnd` >= SPECIFICDATE()))
            OR ((`maintenances`.`dateStart` <= SPECIFICDATE())
            AND (`maintenances`.`dateEnd` > SPECIFICDATE())))
CREATE 
    ALGORITHM = UNDEFINED 
    DEFINER = `root`@`localhost` 
    SQL SECURITY DEFINER
VIEW `shifts_uncoupled` AS
    SELECT 
        `shifts_lists`.`id` AS `id`,
        `shifts_lists`.`date` AS `date`,
        `shifts_lists`.`shifts_options_id` AS `shifts_options_id`,
        `shifts_lists`.`nurses_id` AS `nurses_id`,
        `shifts_lists`.`nurse_student` AS `nurse_student`,
        `shifts_lists`.`drivers_id` AS `drivers_id`,
        `shifts_lists`.`driver_student` AS `driver_student`,
        `shifts_lists`.`ambulances_id` AS `ambulances_id`,
        `shifts_lists`.`created` AS `created`,
        `shifts_lists`.`modified` AS `modified`
    FROM
        (`shifts_lists`
        JOIN `shifts_options` ON ((`shifts_lists`.`shifts_options_id` = `shifts_options`.`id`)))
    WHERE
        (`shifts_lists`.`ambulances_id` IS NULL)
    ORDER BY `shifts_options`.`locations_id` , `shifts_options`.`types_id` , `shifts_options`.`timeStart` , `shifts_lists`.`shifts_options_id`
CREATE TABLE `shifts_lists` (
  `id` int NOT NULL AUTO_INCREMENT,
  `date` date NOT NULL,
  `shifts_options_id` varchar(15) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `nurses_id` int NOT NULL,
  `nurse_student` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
  `drivers_id` int NOT NULL,
  `driver_student` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
  `ambulances_id` int DEFAULT NULL,
  `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=106 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

样本数据:

INSERT INTO `ambulances` (`id`, `licenseplate`, `types_id`, `locations_id`, `created`, `modified`) VALUES
(10001, 'A-123-BC', 1, 1, '2023-10-03 13:53:20', '2023-10-25 01:50:50'),
(10002, 'A-124-BC', 1, 1, '2023-10-03 13:54:44', '2023-10-25 01:51:16'),
(10003, 'A-125-BC', 1, 1, '2023-10-03 13:54:44', '2023-10-25 01:51:16'),
(10004, 'A-126-BC', 1, 1, '2023-10-04 18:42:00', '2023-10-25 01:51:16'),
(10005, 'A-127-BC', 1, 2, '2023-10-04 18:58:58', '2023-10-25 01:51:30');

INSERT INTO `shifts_options` (`id`, `timeStart`, `timeEnd`, `locations_id`, `types_id`, `created`, `modified`) VALUES
('D10', '07:00:00', '15:00:00', 1, 1, '2023-10-18 09:42:35', '2023-11-07 13:58:52'),
('D70', '10:00:00', '18:00:00', 1, 1, '2023-10-18 09:42:35', '2023-11-07 13:58:52'),
('A12', '14:00:00', '22:00:00', 1, 1, '2023-10-18 09:42:35', '2023-11-07 13:58:52'),
('A30', '15:00:00', '23:00:00', 1, 1, '2023-10-18 09:42:35', '2023-11-07 13:58:52'),
('A50', '16:00:00', '00:00:00', 1, 2, '2023-11-15 13:30:18', '2023-11-15 13:30:18');

INSERT INTO `shifts_lists` (`id`, `date`, `shifts_options_id`, `nurses_id`, `nurse_student`, `drivers_id`, `driver_student`, `ambulances_id`, `created`, `modified`) VALUES
(1, '2023-11-06', 'N30', 1109, '', 1716, '', 10004, '2023-11-16 12:00:41', '2023-11-16 12:00:41'),
(2, '2023-11-06', 'N31', 1133, '', 1719, '', 10006, '2023-11-16 12:00:41', '2023-11-16 12:00:41'),
(3, '2023-11-06', 'N40', 1736, 'xxx', 1338, '', 10003, '2023-11-16 12:01:50', '2023-11-16 12:01:50'),
(4, '2023-11-06', 'N41', 1309, '', 1730, '', 10002, '2023-11-16 12:01:50', '2023-11-27 13:11:50'),
(5, '2023-11-06', 'N45', 1756, '', 1184, '', 10005, '2023-11-16 12:02:49', '2023-11-16 12:02:49');
mysql mysql-workbench
1个回答
0
投票

我认为您应该检查

ambulances_shift_available
视图是否正确列出了每个班次的所有可用救护车,并确保此视图提供
shifts_options.id
作为其列之一,该列将用于连接班次数据。

我创建了

ambulances_available_count
视图,该视图应将
ambulances_shift_available
shifts_uncoupled
或相关班次表/视图连接起来并执行计数聚合。

   CREATE VIEW `ambulances_available_count` AS
    SELECT 
        su.`id`,
        su.`date`,
        su.`shifts_options_id`,
        COUNT(asa.`ambulances_id`) AS available_ambulances_count
    FROM
        `shifts_uncoupled` su
    LEFT JOIN
        `ambulances_shift_available` asa ON su.`shifts_options_id` = asa.`id`
    GROUP BY
        su.`id`, su.`date`, su.`shifts_options_id`;
© www.soinside.com 2019 - 2024. All rights reserved.