优化以下这个试图,把2023-06-09作为变量传入,SELECT A.office_id AS distributorId, A.customer_code AS customerCode, A.erp_name AS customerName, A.total_amount AS totalAmount, A.trade_date AS tradeDate, A.customer_type AS customerType, "初次" AS is_first_deal FROM t_first_feedback A WHERE A.`status` = '已成交' AND A.trade_date IS NOT NULL AND A.trade_date >= DATE_FORMAT( DATE_SUB( '2023-06-09', INTERVAL 1 MONTH ), '%Y-%m-01' ) AND A.trade_date <= LAST_DAY( DATE_SUB( '2023-06-09', INTERVAL 1 MONTH )) UNION ALL SELECT B.office_id AS distributorId, B.customer_number AS customerCode, B.customer_name AS customerName, B.total_amount AS totalAmount, B.transaction_date AS tradeDate, "" AS customerType, "重复" AS is_first_deal FROM t_repeat_feedback B WHERE B.transaction_date IS NOT NULL AND B.transaction_date >= DATE_FORMAT( DATE_SUB( '2023-06-09', INTERVAL 1 MONTH ), '%Y-%m-01' ) AND B.transaction_date <= LAST_DAY( DATE_SUB( '2023-06-09', INTERVAL 1 MONTH ))
时间: 2024-02-14 13:14:43 浏览: 71
matlabcodebnb20-bnbgui20-16Nov2001.zip_变量优化_离散 优化_离散变量_离散变量优化_连
5星 · 资源好评率100%
可以将'2023-06-09'作为参数传入SQL语句,避免硬编码,提高代码的可维护性和复用性。具体实现方式如下:
```
DECLARE @date DATE = '2023-06-09';
SELECT A.office_id AS distributorId,
A.customer_code AS customerCode,
A.erp_name AS customerName,
A.total_amount AS totalAmount,
A.trade_date AS tradeDate,
A.customer_type AS customerType,
"初次" AS is_first_deal
FROM t_first_feedback A
WHERE A.`status` = '已成交'
AND A.trade_date IS NOT NULL
AND A.trade_date >= DATE_FORMAT(DATE_SUB(@date, INTERVAL 1 MONTH), '%Y-%m-01')
AND A.trade_date <= LAST_DAY(DATE_SUB(@date, INTERVAL 1 MONTH))
UNION ALL
SELECT B.office_id AS distributorId,
B.customer_number AS customerCode,
B.customer_name AS customerName,
B.total_amount AS totalAmount,
B.transaction_date AS tradeDate,
"" AS customerType,
"重复" AS is_first_deal
FROM t_repeat_feedback B
WHERE B.transaction_date IS NOT NULL
AND B.transaction_date >= DATE_FORMAT(DATE_SUB(@date, INTERVAL 1 MONTH), '%Y-%m-01')
AND B.transaction_date <= LAST_DAY(DATE_SUB(@date, INTERVAL 1 MONTH));
```
阅读全文