开发者

Conversion of IF SQL from MySQL to PostgreSQL

开发者 https://www.devze.com 2022-12-22 17:18 出处:网络
I\'m converting a PHP script as DB has been switched from MySQL to PostgreSQL. I know PG doe开发者_运维技巧sn\'t have the IF function but does have the CASE function. What is the best way to convert

I'm converting a PHP script as DB has been switched from MySQL to PostgreSQL.

I know PG doe开发者_运维技巧sn't have the IF function but does have the CASE function. What is the best way to convert this MySQL statement?

  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


Something like this should work:

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
0

精彩评论

暂无评论...
验证码 换一张
取 消