MYSQL - 存储过程查询简化

Posted

技术标签:

【中文标题】MYSQL - 存储过程查询简化【英文标题】:MYSQL - Stored Procedure Query Simplify 【发布时间】:2021-01-27 09:34:53 【问题描述】:

我是 mysql 的新手。

我需要自定义此查询。现在我只使用了IF条件,查询长度太大了。

在这个存储过程中,我们仅根据 _status 值更改 'f.status' 值

前:

__status = '全部'

ON f.financeId = t.financeId where f.status=0;

__status = '付费'

ON f.financeId = t.financeId where f.status=1;

__status = '未付费'

开启 f.financeId = t.financeId 其中 f.status=2;

查询:

DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `all_data_search`(
_status VARCHAR(50)
)
BEGIN
    IF (_status='All') THEN
    SELECT
    f.financeId,
    f.customerId,
    c.customerName,
    c.initial,
    c.phone1,
    c.aadhaar,
    f.financeStartDate,
    f.vehicleNumber,
    f.loanAmount,
    f.totalInstallment,
    f.installmentAmount,
    COALESCE(t.payedInstallment, 0) as payedInstallment,
    f.status,
    f.created_by,
    f.created_date,
    f.updated_by,
    f.updated_date
FROM finance f
INNER JOIN customer c
    ON c.customerId = f.customerId
LEFT JOIN
(
    SELECT financeId, COUNT(status) AS payedInstallment
    FROM financeinstallment
    WHERE status = 1
    GROUP BY financeId
) t
    ON f.financeId = t.financeId;
    END IF;
    IF (_status='0') THEN
        SELECT
    f.financeId,
    f.customerId,
    c.customerName,
    c.initial,
    c.phone1,
    c.aadhaar,
    f.financeStartDate,
    f.vehicleNumber,
    f.loanAmount,
    f.totalInstallment,
    f.installmentAmount,
    COALESCE(t.payedInstallment, 0) as payedInstallment,
    f.status,
    f.created_by,
    f.created_date,
    f.updated_by,
    f.updated_date
FROM finance f
INNER JOIN customer c
    ON c.customerId = f.customerId
LEFT JOIN
(
    SELECT financeId, COUNT(status) AS payedInstallment
    FROM financeinstallment
    WHERE status = 1
    GROUP BY financeId
) t
    ON f.financeId = t.financeId where f.status=0;
    END IF;
    IF (_status = 'PAID') THEN
    SELECT
    f.financeId,
    f.customerId,
    c.customerName,
    c.initial,
    c.phone1,
    c.aadhaar,
    f.financeStartDate,
    f.vehicleNumber,
    f.loanAmount,
    f.totalInstallment,
    f.installmentAmount,
    COALESCE(t.payedInstallment, 0) as payedInstallment,
    f.status,
    f.created_by,
    f.created_date,
    f.updated_by,
    f.updated_date
FROM finance f
INNER JOIN customer c
    ON c.customerId = f.customerId
LEFT JOIN
(
    SELECT financeId, COUNT(status) AS payedInstallment
    FROM financeinstallment
    WHERE status = 1
    GROUP BY financeId
) t
    ON f.financeId = t.financeId where f.status=1;
     END IF;
     IF (_status = 'UNPAID') THEN
    SELECT
    f.financeId,
    f.customerId,
    c.customerName,
    c.initial,
    c.phone1,
    c.aadhaar,
    f.financeStartDate,
    f.vehicleNumber,
    f.loanAmount,
    f.totalInstallment,
    f.installmentAmount,
    COALESCE(t.payedInstallment, 0) as payedInstallment,
    f.status,
    f.created_by,
    f.created_date,
    f.updated_by,
    f.updated_date
FROM finance f
INNER JOIN customer c
    ON c.customerId = f.customerId
LEFT JOIN
(
    SELECT financeId, COUNT(status) AS payedInstallment
    FROM financeinstallment
    WHERE status = 1
    GROUP BY financeId
) t
    ON f.financeId = t.financeId where f.status=2;
     END IF;
END ;;
DELIMITER ;

【问题讨论】:

优化还是简化? 简化………… ON f.financeId = t.financeId where f.status=FIND_IN_SET(_status, 'ALL,PAID,UNPAID') - 1; 或相同的使用 CASE 函数。 【参考方案1】:
declare status_flag smallint;
....

 IF (_status = 'ALL') THEN status_flag=0;
 ELSEIF (_status = 'PAID') THEN status_flag=1;
 ELSEIF (_status = 'UNPAID') THEN status_flag=2;
 ENDIF;

 ....
 WHERE status = status_flag

【讨论】:

变量过多,没有利润。直接在查询中使用 CASE 函数更有用。 Akina:你能回答我的问题吗 我需要动态的 Just WHERE 条件。【参考方案2】:

您可以使用布尔运算如下:

ON f.financeId = t.financeId 
where (__status = 'ALL' and f.status=0)
   or (__status = 'PAID' and f.status=1)
   or (__status = 'UNPAID' and f.status=2);

【讨论】:

以上是关于MYSQL - 存储过程查询简化的主要内容,如果未能解决你的问题,请参考以下文章

MySQL基础--存储过程和函数

lyt经典版MySQL基础——存储过程

mysql-存储过程和函数-热身运动

MySQL存储过程和函数

MySQL存储过程

怎么在mysql中查询已建立的存储过程