如何使函数在 postgreSQL 中使用 CASE 和 IFNULL?

How to make a function use CASE and IFNULL in postgreSQL?

你能告诉我如何在 postgre 中使用 CASE 和 IFNULL 吗?我正在从 sql 迁移到 postgreSQL,我想用这种语法创建一个函数:

SELECT a.furniture_id, a.furniture_brand, a.furniture_cost, 
CASE 
  WHEN furniture_id = f_id THEN a.furniture_content 
  ELSE '' 
END CONTENT 
   FROM tb_furniture a
   WHERE 
    (IFNULL(a.sale_id,0) = IFNULL(f_id,0) OR (a.furniture_id = f_Id AND IFNULL(a.furniture_content,'') > '')) 
    AND a.published = 1;

之前谢谢你(y)

这似乎是你想要的,但仔细检查 WHERE 条件:

SELECT a.furniture_id, a.furniture_brand, a.furniture_cost, 
       CASE WHEN a.furniture_id = f_id
       THEN a.furniture_content 
       ELSE '' 
       END AS content
FROM tb_furniture a
WHERE (coalesce(a.sale_id,0) = coalesce(f_id,0) OR length(content) > 0)
  AND a.published = 1;