SQL select return 0 if no records found, else return value

后端 未结 4 626
再見小時候
再見小時候 2021-01-24 18:02

I am now using Microsoft SQL, my code is:

   SELECT TOP 1 
   [avail]
   FROM [table1]
   where [name] = \'abc\'
   order by [datetime] desc

I

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-24 18:43

    You can use this

    SELECT ISNULL(( SELECT TOP 1 
       [avail]
       FROM [table1]
       where [name] = 'abc'
       order by [datetime] desc), 0) AS [avail]
    

提交回复
热议问题