mysql - 如何在 MYSQL 存储过程 WHERE 子句中添加 IF ELSE IF 以在参数为 NULL 时不运行 WHERE 内的 AND

标签 mysql stored-procedures

DELIMITER $

CREATE PROCEDURE filing_route(IN appl_name varchar(500),
IN granted char(1),
IN oppose tinyint(4),
IN beginDate date,
IN endDate date,
IN ipc varchar(300))
BEGIN

DROP TABLE IF EXISTS total_company_applications;
DROP TABLE IF EXISTS family_aggregate;
DROP TABLE IF EXISTS tmp_filing_route;
DROP TABLE IF EXISTS tmp_filing_route2;

IF (granted IS NOT NULL AND oppose IS NOT NULL AND beginDate IS NOT NULL AND endDate IS NOT NULL AND ipc IS NOT NULL) THEN
    CREATE TEMPORARY TABLE IF NOT EXISTS total_company_applications AS
    (SELECT distinct a.appln_id FROM sample.tls201_appln a
    INNER JOIN sample.tls207_pers_appln b ON b.appln_id = a.appln_id
    INNER JOIN sample.tls906_person c ON c.person_id = b.person_id
    INNER JOIN sample.tls209_appln_ipc d ON d.appln_id = a.appln_id
    WHERE c.psn_name LIKE CONCAT(appl_name, '%')
    AND a.granted LIKE granted
    AND a.isOpposed = oppose
    AND a.appln_filing_date BETWEEN beginDate and endDate
    AND FIND_IN_SET(d.ipc_class_symbol, ipc));
ELSE IF(oppose IS NOT NULL AND beginDate IS NOT NULL AND endDate IS NOT NULL AND ipc IS NOT NULL) THEN
    CREATE TEMPORARY TABLE IF NOT EXISTS total_company_applications AS
    (SELECT distinct a.appln_id FROM sample.tls201_appln a
    INNER JOIN sample.tls207_pers_appln b ON b.appln_id = a.appln_id
    INNER JOIN sample.tls906_person c ON c.person_id = b.person_id
    INNER JOIN sample.tls209_appln_ipc d ON d.appln_id = a.appln_id
    WHERE c.psn_name LIKE CONCAT(appl_name, '%')
    AND a.granted LIKE granted
    AND a.isOpposed = oppose
    AND a.appln_filing_date BETWEEN beginDate and endDate
    AND FIND_IN_SET(d.ipc_class_symbol, ipc));

CREATE TEMPORARY TABLE IF NOT EXISTS family_aggregate AS 
(SELECT b.docdb_family_id, b.appln_auth, b.receiving_office, MIN(b.appln_filing_date) as first_date 
FROM total_company_applications a
INNER JOIN tls201_appln b ON b.appln_id = a.appln_id 
WHERE b.appln_filing_year < 9999
GROUP BY b.docdb_family_id, b.appln_auth, b.receiving_office
ORDER BY b.docdb_family_id, first_date);

CREATE TEMPORARY TABLE IF NOT EXISTS tmp_filing_route AS (
SELECT docdb_family_id, first_date, group_concat(distinct appln_auth, IF(appln_auth = 'WO', CONCAT(' (RO = ', receiving_office, ')'), '') ORDER BY appln_auth DESC separator ', ') as filing_route 
FROM family_aggregate
GROUP BY docdb_family_id, first_date);

CREATE TEMPORARY TABLE IF NOT EXISTS tmp_filing_route2 AS (
SELECT docdb_family_id, group_concat(filing_route ORDER BY first_date separator ' -> ') as filing_route
FROM tmp_filing_route
GROUP BY docdb_family_id);

SELECT filing_route, COUNT(distinct docdb_family_id) as num_families
FROM tmp_filing_route2
GROUP BY filing_route
ORDER BY num_families DESC
LIMIT 20;


END$

我有一个存储过程filing_route,如果参数为空,我不想运行带有AND 和参数Where 的行。 我试过这个

WHERE c.psn_name LIKE CONCAT(appl_name, '%')
IF(granted IS NOT NULL)
    AND a.granted LIKE granted
END IF
AND a.isOpposed = oppose

但是它不起作用。如果未给出参数或参数为空,我可以获得有关如何设置默认情况的建议吗?如果我执行之前的解决方法,即对所有 5 种情况进行组合。我必须建立 2^5=64 个案例来检查。是否有更聪明的方法来做到这一点。

最佳答案

对于这个问题

No I want to know that how can I add AND only if the parameter exists

你可以尝试下面的方法,只需使用IF(),关键是当条件不匹配时只需添加1=1即可使sql语法正确

WHERE c.psn_name LIKE CONCAT(appl_name, '%')
  AND a.isOpposed = oppose
  AND IF(granted IS NOT NULL,a.granted LIKE granted,1=1)

关于mysql - 如何在 MYSQL 存储过程 WHERE 子句中添加 IF ELSE IF 以在参数为 NULL 时不运行 WHERE 内的 AND,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53629717/

相关文章:

mysql - 通知数据库上的应用程序 INSERT

sql-server - 如何找出哪个存储过程的哪一行发生了错误?

SQL Server 存储过程因使用 XML/ANSI_NULLS、QUOTED_IDENTIFIER 选项而失败

mysql - 如何在 MySQL 函数中引发错误

oracle - 使用 pl/sql 过程记录错误并处理异常

mysql - 数据库布局问题

mysql - sudo yum install postfix mysql-libs 报错

mysql - 根据特定日期集从 MySQL 获取数据

Java Applet 显示数据库中的名称

mysql - 存储过程中 RLIKE 和 LIKE 的奇怪行为