如何将参数传递给具有联合的mysql存储过程
Posted
技术标签:
【中文标题】如何将参数传递给具有联合的mysql存储过程【英文标题】:How to pass parameters to mysql stored procedure having union 【发布时间】:2018-03-02 03:45:15 【问题描述】:我是 mysql 的新手,并试图在存储过程中使用 IF statement
和 UNION ALL
编写 Stored Procedure
。但我不确定在查询中在哪里添加Where Condition
。
我在这里尝试:
BEGIN
SELECT 'ele_certificate' AS CERTIFICATE, `ele_certificate` AS STATUS, `property_id` AS pid, `ELEcertificate_issue_date` AS OldExpiry, `ELEcertificate_expiry_date` AS NewExpiry, `ELEcertificate_duration` AS Duration, `ELEcertNotes` AS Notes, `property_code`, `property_added_date`, `property_address_1`, `property_address_2`, `property_address_3`, `property_city`, `property_cluster`, `property_area`, `property_postcode`, `property_landlord_id`, `property_status`, CONCAT(tbl_landlord.landlord_first_name,' ',tbl_landlord.landlord_middle_name,' ',tbl_landlord.landlord_last_name) AS fullName, tbl_landlord.landlord_work_number, tbl_landlord.landlord_mobile_number, tbl_landlord.landlord_main_email
FROM tbl_property LEFT OUTER JOIN tbl_landlord ON tbl_property.property_landlord_id=tbl_landlord.landlord_id
UNION ALL
SELECT 'eml_certificate', `eml_certificate`, `property_id`, `EMLcertificate_issue_date`, `EMLcertificate_expiry_date`, `EMLcertificate_duration`, `EMLcertNotes`, `property_code`, `property_added_date`, `property_address_1`, `property_address_2`, `property_address_3`, `property_city`, `property_cluster`, `property_area`, `property_postcode`, `property_landlord_id`, `property_status`, CONCAT(tbl_landlord.landlord_first_name,' ',tbl_landlord.landlord_middle_name,' ',tbl_landlord.landlord_last_name) AS fullName, tbl_landlord.landlord_work_number, tbl_landlord.landlord_mobile_number, tbl_landlord.landlord_main_email
FROM tbl_property LEFT OUTER JOIN tbl_landlord ON tbl_property.property_landlord_id=tbl_landlord.landlord_id
UNION ALL
SELECT 'epc_certificate', `epc_certificate`, `property_id`, `EPCcertificate_issue_date`, `EPCcertificate_expiry_date`, `EPCcertificate_duration`, `EPCcertNotes`, `property_code`, `property_added_date`, `property_address_1`, `property_address_2`, `property_address_3`, `property_city`, `property_cluster`, `property_area`, `property_postcode`, `property_landlord_id`, `property_status`, CONCAT(tbl_landlord.landlord_first_name,' ',tbl_landlord.landlord_middle_name,' ',tbl_landlord.landlord_last_name) AS fullName, tbl_landlord.landlord_work_number, tbl_landlord.landlord_mobile_number, tbl_landlord.landlord_main_email
FROM tbl_property LEFT OUTER JOIN tbl_landlord ON tbl_property.property_landlord_id=tbl_landlord.landlord_id
UNION ALL
SELECT 'fir_certificate', `fir_certificate`, `property_id`, `FIRcertificate_issue_date`, `FIRcertificate_expiry_date`, `FIRcertificate_duration`, `FIRcertNotes`, `property_code`, `property_added_date`, `property_address_1`, `property_address_2`, `property_address_3`, `property_city`, `property_cluster`, `property_area`, `property_postcode`, `property_landlord_id`, `property_status`, CONCAT(tbl_landlord.landlord_first_name,' ',tbl_landlord.landlord_middle_name,' ',tbl_landlord.landlord_last_name) AS fullName, tbl_landlord.landlord_work_number, tbl_landlord.landlord_mobile_number, tbl_landlord.landlord_main_email
FROM tbl_property LEFT OUTER JOIN tbl_landlord ON tbl_property.property_landlord_id=tbl_landlord.landlord_id
UNION ALL
SELECT 'gas_certificate', `gas_certificate`, `property_id`, `GAScertificate_issue_date`, `GAScertificate_expiry_date`, `GAScertificate_duration`, `GAScertNotes`, `property_code`, `property_added_date`, `property_address_1`, `property_address_2`, `property_address_3`, `property_city`, `property_cluster`, `property_area`, `property_postcode`, `property_landlord_id`, `property_status`, CONCAT(tbl_landlord.landlord_first_name,' ',tbl_landlord.landlord_middle_name,' ',tbl_landlord.landlord_last_name) AS fullName, tbl_landlord.landlord_work_number, tbl_landlord.landlord_mobile_number, tbl_landlord.landlord_main_email
FROM tbl_property LEFT OUTER JOIN tbl_landlord ON tbl_property.property_landlord_id=tbl_landlord.landlord_id
UNION ALL
SELECT 'hmo_certificate', `hmo_certificate`, `property_id`, `HMOcertificate_issue_date`, `HMOcertificate_expiry_date`, `HMOcertificate_duration`, `HMOcertNotes`, `property_code`, `property_added_date`, `property_address_1`, `property_address_2`, `property_address_3`, `property_city`, `property_cluster`, `property_area`, `property_postcode`, `property_landlord_id`, `property_status`, CONCAT(tbl_landlord.landlord_first_name,' ',tbl_landlord.landlord_middle_name,' ',tbl_landlord.landlord_last_name) AS fullName, tbl_landlord.landlord_work_number, tbl_landlord.landlord_mobile_number, tbl_landlord.landlord_main_email
FROM tbl_property LEFT OUTER JOIN tbl_landlord ON tbl_property.property_landlord_id=tbl_landlord.landlord_id
UNION ALL
SELECT 'ins_certificate', `ins_certificate`, `property_id`, `INScertificate_issue_date`, `INScertificate_expiry_date`, `INScertificate_duration`, `INScertNotes`, `property_code`, `property_added_date`, `property_address_1`, `property_address_2`, `property_address_3`, `property_city`, `property_cluster`, `property_area`, `property_postcode`, `property_landlord_id`, `property_status`, CONCAT(tbl_landlord.landlord_first_name,' ',tbl_landlord.landlord_middle_name,' ',tbl_landlord.landlord_last_name) AS fullName, tbl_landlord.landlord_work_number, tbl_landlord.landlord_mobile_number, tbl_landlord.landlord_main_email
FROM tbl_property LEFT OUTER JOIN tbl_landlord ON tbl_property.property_landlord_id=tbl_landlord.landlord_id;
END
这个过程给了我正确的结果,我在单个表上应用了 UNION ALL 来将列转换为行。
当我尝试将参数传递给程序时,我正在编写以下 Query
:
BEGIN
SET @Certificates = ("SELECT 'ele_certificate' AS CERTIFICATE, `ele_certificate` AS STATUS, `property_id` AS pid, `ELEcertificate_issue_date` AS OldExpiry, `ELEcertificate_expiry_date` AS NewExpiry, `ELEcertificate_duration` AS Duration, `ELEcertNotes` AS Notes, `property_code`, `property_added_date`, `property_address_1`, `property_address_2`, `property_address_3`, `property_city`, `property_cluster`, `property_area`, `property_postcode`, `property_landlord_id`, `property_status`, CONCAT(tbl_landlord.landlord_first_name,' ',tbl_landlord.landlord_middle_name,' ',tbl_landlord.landlord_last_name) AS fullName, tbl_landlord.landlord_work_number, tbl_landlord.landlord_mobile_number, tbl_landlord.landlord_main_email
FROM tbl_property LEFT OUTER JOIN tbl_landlord ON tbl_property.property_landlord_id=tbl_landlord.landlord_id
UNION ALL
SELECT 'eml_certificate', `eml_certificate`, `property_id`, `EMLcertificate_issue_date`, `EMLcertificate_expiry_date`, `EMLcertificate_duration`, `EMLcertNotes`, `property_code`, `property_added_date`, `property_address_1`, `property_address_2`, `property_address_3`, `property_city`, `property_cluster`, `property_area`, `property_postcode`, `property_landlord_id`, `property_status`, CONCAT(tbl_landlord.landlord_first_name,' ',tbl_landlord.landlord_middle_name,' ',tbl_landlord.landlord_last_name) AS fullName, tbl_landlord.landlord_work_number, tbl_landlord.landlord_mobile_number, tbl_landlord.landlord_main_email
FROM tbl_property LEFT OUTER JOIN tbl_landlord ON tbl_property.property_landlord_id=tbl_landlord.landlord_id
UNION ALL
SELECT 'epc_certificate', `epc_certificate`, `property_id`, `EPCcertificate_issue_date`, `EPCcertificate_expiry_date`, `EPCcertificate_duration`, `EPCcertNotes`, `property_code`, `property_added_date`, `property_address_1`, `property_address_2`, `property_address_3`, `property_city`, `property_cluster`, `property_area`, `property_postcode`, `property_landlord_id`, `property_status`, CONCAT(tbl_landlord.landlord_first_name,' ',tbl_landlord.landlord_middle_name,' ',tbl_landlord.landlord_last_name) AS fullName, tbl_landlord.landlord_work_number, tbl_landlord.landlord_mobile_number, tbl_landlord.landlord_main_email
FROM tbl_property LEFT OUTER JOIN tbl_landlord ON tbl_property.property_landlord_id=tbl_landlord.landlord_id
UNION ALL
SELECT 'fir_certificate', `fir_certificate`, `property_id`, `FIRcertificate_issue_date`, `FIRcertificate_expiry_date`, `FIRcertificate_duration`, `FIRcertNotes`, `property_code`, `property_added_date`, `property_address_1`, `property_address_2`, `property_address_3`, `property_city`, `property_cluster`, `property_area`, `property_postcode`, `property_landlord_id`, `property_status`, CONCAT(tbl_landlord.landlord_first_name,' ',tbl_landlord.landlord_middle_name,' ',tbl_landlord.landlord_last_name) AS fullName, tbl_landlord.landlord_work_number, tbl_landlord.landlord_mobile_number, tbl_landlord.landlord_main_email
FROM tbl_property LEFT OUTER JOIN tbl_landlord ON tbl_property.property_landlord_id=tbl_landlord.landlord_id
UNION ALL
SELECT 'gas_certificate', `gas_certificate`, `property_id`, `GAScertificate_issue_date`, `GAScertificate_expiry_date`, `GAScertificate_duration`, `GAScertNotes`, `property_code`, `property_added_date`, `property_address_1`, `property_address_2`, `property_address_3`, `property_city`, `property_cluster`, `property_area`, `property_postcode`, `property_landlord_id`, `property_status`, CONCAT(tbl_landlord.landlord_first_name,' ',tbl_landlord.landlord_middle_name,' ',tbl_landlord.landlord_last_name) AS fullName, tbl_landlord.landlord_work_number, tbl_landlord.landlord_mobile_number, tbl_landlord.landlord_main_email
FROM tbl_property LEFT OUTER JOIN tbl_landlord ON tbl_property.property_landlord_id=tbl_landlord.landlord_id
UNION ALL
SELECT 'hmo_certificate', `hmo_certificate`, `property_id`, `HMOcertificate_issue_date`, `HMOcertificate_expiry_date`, `HMOcertificate_duration`, `HMOcertNotes`, `property_code`, `property_added_date`, `property_address_1`, `property_address_2`, `property_address_3`, `property_city`, `property_cluster`, `property_area`, `property_postcode`, `property_landlord_id`, `property_status`, CONCAT(tbl_landlord.landlord_first_name,' ',tbl_landlord.landlord_middle_name,' ',tbl_landlord.landlord_last_name) AS fullName, tbl_landlord.landlord_work_number, tbl_landlord.landlord_mobile_number, tbl_landlord.landlord_main_email
FROM tbl_property LEFT OUTER JOIN tbl_landlord ON tbl_property.property_landlord_id=tbl_landlord.landlord_id
UNION ALL
SELECT 'ins_certificate', `ins_certificate`, `property_id`, `INScertificate_issue_date`, `INScertificate_expiry_date`, `INScertificate_duration`, `INScertNotes`, `property_code`, `property_added_date`, `property_address_1`, `property_address_2`, `property_address_3`, `property_city`, `property_cluster`, `property_area`, `property_postcode`, `property_landlord_id`, `property_status`, CONCAT(tbl_landlord.landlord_first_name,' ',tbl_landlord.landlord_middle_name,' ',tbl_landlord.landlord_last_name) AS fullName, tbl_landlord.landlord_work_number, tbl_landlord.landlord_mobile_number, tbl_landlord.landlord_main_email
FROM tbl_property LEFT OUTER JOIN tbl_landlord ON tbl_property.property_landlord_id=tbl_landlord.landlord_id");
IF datepickerfilter1 != 0 AND datepickerfilter2 != 0 THEN
SELECT @Certificates + WHERE
ELEcertificate_expiry_date BEGIN datepickerfilter1 AND datepickerfilter2 OR
EMLcertificate_expiry_date BEGIN datepickerfilter1 AND datepickerfilter2 OR
EPCcertificate_expiry_date BEGIN datepickerfilter1 AND datepickerfilter2 OR
FIRcertificate_expiry_date BEGIN datepickerfilter1 AND datepickerfilter2 OR
GAScertificate_expiry_date BEGIN datepickerfilter1 AND datepickerfilter2 OR
HMOcertificate_expiry_date BEGIN datepickerfilter1 AND datepickerfilter2 OR
INScertificate_expiry_date BEGIN datepickerfilter1 AND datepickerfilter2;
PREPARE smtp FROM @Certificates;
EXECUTE smtp;
END IF;
IF datepickerfilter1 != 0 AND datepickerfilter2 = 0 THEN
SELECT @Certificates + WHERE
ELEcertificate_expiry_date = datepickerfilter1 OR EMLcertificate_expiry_date = datepickerfilter1 OR EPCcertificate_expiry_date = datepickerfilter1 OR FIRcertificate_expiry_date = datepickerfilter1 OR GAScertificate_expiry_date = datepickerfilter1 OR HMOcertificate_expiry_date = datepickerfilter1 OR INScertificate_expiry_date = datepickerfilter1;
PREPARE smtp FROM @Certificates;
EXECUTE smtp;
END IF;
IF datepickerfilter1 = 0 AND datepickerfilter2 != 0 THEN
SELECT @Certificates + WHERE
ELEcertificate_expiry_date = datepickerfilter2 OR EMLcertificate_expiry_date = datepickerfilter2 OR EPCcertificate_expiry_date = datepickerfilter2 OR FIRcertificate_expiry_date = datepickerfilter2 OR GAScertificate_expiry_date = datepickerfilter2 OR HMOcertificate_expiry_date = datepickerfilter2 OR INScertificate_expiry_date = datepickerfilter2;
PREPARE smtp FROM @Certificates;
EXECUTE smtp;
END IF;
IF datepickerfilter1 = 0 AND datepickerfilter2 != 0 THEN
SELECT @Certificates;
PREPARE smtp FROM @Certificates;
EXECUTE smtp;
END IF;
END
在此我尝试将之前的结果存储在一个变量中,然后应用IF condition
。但是我没有执行该过程。
注意:
我在单个表中有 7 个不同的 expiry_date
列,通过应用 UNION ALL
我将其转换为单个列。现在我试图通过将参数传递给结果输出来过滤数据。
我用谷歌搜索了它,但没有得到正确的解决方案,欢迎任何形式的帮助,在此先感谢。
【问题讨论】:
【参考方案1】:局部变量(以@开头)在运行时不求值,您必须使用常规字符串变量来存储 SQL,然后再创建准备好的语句。
像这样构建您的 @certificates 变量 ussign concat(...)
:
IF datepickerfilter1 <> 0 AND datepickerfilter2 = 0 THEN
set @Certificates = concat(@Certificates,
" WHERE ELEcertificate_expiry_date ",
" BETWEEN '",datepickerfilter1,"' AND '",datepickerfilter2, "'" );
/*I'm using one line as example, you must complete to meet your query*/
PREPARE smtp FROM @Certificates;
EXECUTE smtp;
DEALLOCATE PREPARE smtp; -- important
END IF;
对其他准备好的语句执行相同的操作。
【讨论】:
以上是关于如何将参数传递给具有联合的mysql存储过程的主要内容,如果未能解决你的问题,请参考以下文章
如何将参数传递给 Microsoft Sync 2.1 生成的存储过程