dsx666666
dsx666666
2016-07-12 13:56

SQL查询If语句?

已采纳

I have two fields in a legacy database. I want to query one of them, but its often null, In this case is there like an if statement like Where if field_a !== null { field_a='Value' } else { field_b='Value' }

My Question is, is there a way to do this in sql itself or would i have to do two queries, one to check if null, then an if statement in my logic?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dongzi0602 dongzi0602 5年前

    I think you can capture the logic like this:

    where field_a = 'Value' or (field_a is NULL and field_b = 'Value')
    

    or more simply as:

    where field_a = 'Value' or field_b = 'Value'
    
    点赞 评论 复制链接分享