[英]MYSQL - Stored Procedure Query Simplify
我是 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 = '未付'
ON 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 ;
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
您可以使用 boolean 操作如下:
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.