我需要关于方法或算法或工具的建议来满足我的需求; SQL中的动态过滤。
我使用PHP作为后端。我在前端创建结果过滤,如下所示:
单击“确定”后,我使用以下命令发送JSON数据:
JSON.stringify($('#modalFilter input, #modalFilter select').serializeArray())
这是数据:
"[
{"name":"parameter","value":"state"},
{"name":"operator","value":"contains"},
{"name":"value","value":"mel"},
{"name":"parameter","value":"dateappoint"},
{"name":"operator","value":"begin"},
{"name":"value","value":"13/12/2017"}
]"
我的问题在这里。在后端(PHP),经过一些按摩(绑定参数,执行查询等)后,我出现了一个非常庞大和丑陋的 SQL:
SELECT ...
FROM sample_staff
WHERE
CASE WHEN 'state_contains' = ? THEN state LIKE CONCAT('%',?,'%') END
CASE WHEN 'state_notcontains' = ? THEN state NOT LIKE CONCAT('%',?,'%') END
CASE WHEN 'state_is' = ? THEN state = ? END
CASE WHEN 'state_isnt' = ? THEN state <> ? END
CASE WHEN 'state_begin' = ? THEN state LIKE CONCAT(?,'%') END
CASE WHEN 'state_end' = ? THEN state LIKE CONCAT('%',?) END
CASE WHEN 'dateappoint_contains' = ? THEN dateappoint LIKE CONCAT('%',?,'%') END
CASE WHEN 'dateappoint_notcontains' = ? THEN dateappoint NOT LIKE CONCAT('%',?,'%') END
CASE WHEN 'dateappoint_is' = ? THEN dateappoint = ? END
CASE WHEN 'dateappoint_isnt' = ? THEN dateappoint <> ? END
CASE WHEN 'dateappoint_begin' = ? THEN dateappoint >= STR_TO_DATE(?, '%d/%m/%y') END
CASE WHEN 'dateappoint_end' = ? THEN dateappoint <= STR_TO_DATE(?, '%d/%m/%y') END
以上示例SQL只能满足2个条件(state和dataappoint),但在实际情况下,我必须提供更多。我希望避免存储过程只是为了循环条件。
我听说过学说,但仍然不确定该工具是否能满足我的需求,或者它的用途完全不同。