mysql - MySQL存储过程:如何缩短语句?
问题描述
只是想就在 MySQL 数据库中创建存储过程向您寻求帮助。这是我第一次创建存储过程。
很可能,我曾经创建视图,但这次,我需要在 mysql 数据库中实现一个存储过程。我在这里创建了一个简单的,但我认为它在声明中是多余的。有没有人可以帮我缩短声明?
有了这个,它可以帮助我学习更多关于存储过程的知识。请参阅代码以供参考。非常感谢您的帮助。
DELIMITER $$
USE `doris_master_data`$$
DROP PROCEDURE IF EXISTS `webserver_service_order`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `webserver_service_order`(
IN xselection BIGINT(8),
IN xreceived_from_date BIGINT(8),
IN xreceived_to_date BIGINT(8))
BEGIN
IF xselection=0 THEN
SELECT DISTINCT
so.service_code,
sl.service_name,
xx.Total_Services
FROM
service_order so
INNER JOIN services_lists sl ON so.service_code=sl.service_code
LEFT OUTER JOIN
(
SELECT
a.service_code,
COUNT(DISTINCT CONCAT(a.service_code,'-',a.service_order_code)) AS Total_Services
FROM
service_order a
WHERE
(a.service_order_date_received BETWEEN xreceived_from_date AND xreceived_to_date)
AND a.service_code_is_active=1
AND a.requirement_code_is_active=1
GROUP BY a.service_code
) AS xx ON so.service_code=xx.service_code
WHERE
(so.service_order_date_received BETWEEN xreceived_from_date AND xreceived_to_date)
AND so.service_code_is_active=1
AND so.requirement_code_is_active=1;
ELSE
SELECT DISTINCT
so.service_code,
sl.service_name,
xx.Total_Services
FROM
service_order so
INNER JOIN services_lists sl ON so.service_code=sl.service_code
LEFT OUTER JOIN
(
SELECT
a.service_code,
COUNT(DISTINCT CONCAT(a.service_code,'-',a.service_order_code)) AS Total_Services
FROM
service_order a
WHERE
(a.service_order_date_received BETWEEN xreceived_from_date AND xreceived_to_date)
AND a.service_code_is_active=1
AND a.requirement_code_is_active=1
GROUP BY a.service_code
) AS xx ON so.service_code=xx.service_code
WHERE
so.service_code=xselection AND
(so.service_order_date_received BETWEEN xreceived_from_date AND xreceived_to_date)
AND so.service_code_is_active=1
AND so.requirement_code_is_active=1;
END IF;
END$$
DELIMITER ;
解决方案
推荐阅读
- python - flask-sqlalchemy 中每组最高的 n
- objective-c - NSButton Push On Push Off 类型
- javascript - 如何保持绝对路径规则从 ts 编译到 js 文件?
- r-markdown - 编织到 Word 文档时在 Rmarkdown 中重置页面计数器
- kubernetes - 如何在 EKS 中编辑 appsettings.json
- python - 在 Python 中创建用于更新记录的第二个表单
- android - 在 Android 中查看针对浏览器与外部应用程序的意图
- react-native - 如何使用“require”为 React bootstrap Card img src 添加一个 prop url
- python - python click -- 动态分组命令全部在一个函数中
- c++ - 使用 pybind11 包装 yaml-cpp 迭代器