美文网首页
sql语句借助case when实现自动拼装where条件

sql语句借助case when实现自动拼装where条件

作者: 不务正业的coder | 来源:发表于2019-07-31 16:54 被阅读0次

遇到这样一个场景,一条SQL语句,根据A字段查询,在没有mybatis情况下,如何实现“如果A字段为空,则查询全量,否则根据A字段过滤。

经过摸索,可以借助case when ... else end 在where中实现。

select   *  from http_summary

where

CASE

    when '$api' != '全部' then to_api='$api'

    else to_api=to_api

END

  and trace_date ='$traceDate'

相关文章

网友评论

      本文标题:sql语句借助case when实现自动拼装where条件

      本文链接:https://www.haomeiwen.com/subject/rgmtdctx.html