文章目錄
問題
最近有個需求,sql如下:
select case when score < 60 then 60 else '優秀' end from stuent
但是運行的時候報錯了:ERROR: invalid input syntax for type numeric:'優秀'
百度說:數據類型不符。
仔細想一下, 60是int,優秀是string,確實類型不符。
sql修改如下:
select case when score < 60 then '' || 60 else '優秀' end from stuent
這樣就都是string了,就不報錯了。
把結果擴展一下
其實這個報錯不只case when會報,其他語句也會報這個錯。原因都是類型不符,只要抓住這個,去排錯就行了。