SELECT SUM returns a row when there are no records

后端 未结 8 1038
囚心锁ツ
囚心锁ツ 2021-01-17 10:38

I\'m finding some problems with a query that returns the sum of a field from a table for all the records that meet certain conditions. I expected to receive a \"No records f

8条回答
  •  离开以前
    2021-01-17 11:11

    "I expected to receive a "No records found' when there were no records, but instead I'm receiving a null result."

    Then do

    SELECT SUM(dummy) FROM DUAL WHERE 1=2 HAVING COUNT(*) > 0
    

    That is, specify that you only want to return a summary where there were rows that were considered.

    SELECT SUM(dummy) FROM DUAL WHERE 1=2 HAVING SUM(dummy) IS NOT NULL
    

    is similar, but the COUNT(*) would return a summary row if there were only rows for which dummy was null, while the latter would not.

提交回复
热议问题