今天写项目接口看到接口文档要求带8个参数3个参数必选5个可选,研究了一下,最后博主在sql中用了AND和OR的语句解决了,不多说,看代码。
判断如果没有相应的可选参数传进来,就把他设为Null
sql语句传?表示预处理,主要看where后面的条件,注意AND和OR的优先级(AND高于OR),博主刚开始没注意到,最后用()来控制优先级
SELECT t.ProjectName,t.Cost,t.IsBuyoff,t.IsTimeOut,t.CreateDate
FROM
(SELECT i.ProjectName,i.Cost,p.IsBuyoff,IF(DATEDIFF(NOW() ,p.CreateTime) > c.DealthDays,1,0) IsTimeOut,i.CreateDate from project_info i JOIN project_process p on i.CurProcessID = p.ProcessID and i.ID = p.ProjectID JOIN process_info c ON i.CurProcessID = c.ID WHERE i.CurProcessID = ?) t
WHERE
(? is null or t.ProjectName = ?)
AND
(? is null or t.Cost = ?)
AND
(? is null or t.IsBuyoff = ?)
AND
(? is null or t.IsTimeOut = ?)
AND
(? is null or t.CreateDate = ?)
LIMIT ?,?
红色框里面的是可选参数,为了与上面?对应,每个可选参数都需要传两遍。
已解决的给博主点赞,没解决的评论说出你的问题