在数据库的查询过程中,遇到以下问题:
当select * from tKeyword where typeId = case when @typeId < 0 then null else @typeId end order by orderNo desc
这句话是正确的,但是当@typeId < 0不出结果,而数据库中有满足条件typeId=null的数据。
后来进行了以下处理:
if @typeId < 0 then
    select * from tKeyword where typeId is null order by orderNo desc
else
    select * from tKeyword where typeId = @typeId order by orderNo desc
问题得到解决了。进一步简化问题如下:
select * from tKeyword where isnull(typeId,@typeId)=@typeId end order by orderNo desc
显示这是一种更为简化的处理方法。

在数据操作时,一定要注意字段的null的处理方法。
好了,以后有机会在写了。

相关文章:

  • 2021-09-13
  • 2022-12-23
  • 2022-12-23
  • 2021-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-15
猜你喜欢
  • 2021-08-02
  • 2022-12-23
  • 2022-12-23
  • 2021-10-03
  • 2021-08-11
  • 2021-10-09
  • 2022-12-23
相关资源
相似解决方案