我正在转换一个php脚本,因为db已经从mysql切换到postgresql。
我知道pg没有if函数,但是有case函数。转换mysql语句的最佳方法是什么?

  SELECT albums.id,
         albums.albumname,
         sum(if(((albums.id=allalbums.albumid) and
             (allalbums.photoid=$photoid)),1,0)) as inalbum
    FROM albums, allalbums
GROUP BY albums.id
ORDER BY albums.createdate desc

最佳答案

这样的做法应该管用:

select
  albums.id,
  albums.albumname,
  sum(
   case when ((albums.id=allalbums.albumid) and (allalbums.photoid=$photoid)) then 1
     else 0
   end
  ) as inalbum
from albums,allalbums
group by albums.id
order by albums.createdate desc

关于sql - 将IF SQL从MySQL转换为PostgreSQL,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2397847/

10-11 08:04