MySQL多个不确定条件查询
程序员文章站
2022-03-01 17:21:14
...
在写项目的时候遇到了以多个条件查询信息的功能,有的条件具有不确定性,是一个下拉框,如果选择全部,就查询全部的信息,选择特定的条件,就按照特定的条件去查询,如下图所示:
SQL语句
SELECT i.id, i.`name`, l.state, c.department caller, l.start_time startTime, l.end_time endTime, (l.end_time - l.start_time) elapsedTime
FROM (SELECT * FROM 表名 ORDER BY start_time DESC) l, 表名 i, 表名 c
WHERE l.interface_id = i.id AND l.caller = c.`code`
AND ('All' = '{interfaceName}' OR i.`name` = '{interfaceName}' )
AND ('All' = '{caller}' OR c.department = '{caller}')
AND ('All' = '{state}' OR l.state = '{state}')
AND l.start_time >= {startTime}
AND l.end_time <= {endTime}
LIMIT {pagination}, {pageSize}