首页 > 解决方案 > 如何创建函数过滤 PostgreSQL 到 MySQL?

问题描述

如何创建函数过滤PostgreSQL 到 MySQL?

我尝试如下,但失败了:

select
    CASE WHEN statusstorie = false and statusTwentyFive = true and category not in ('goals') and space = 'personal' THEN count(*) ELSE 0 END as "25%",
    CASE WHEN statusstorie = false and statusFifty = true and category not in ('goals') and space = 'personal' THEN count(*) ELSE 0 END as "50%",
    CASE WHEN statusstorie = false and statusSeventyFive = true and category not in ('goals') and space = 'personal' THEN count(*) ELSE 0 END as "75%",
    CASE WHEN statusstorie = false and statusOneHundred = true and category not in ('goals') and space = 'personal' THEN count(*) ELSE 0 END as "100%"
from goals 

PSQL 示例:

select
    count(*) filter(where statusstorie = false and statusTwentyFive = true and category not in ('goals') and space = 'personal')  "25%",
    count(*) filter(where statusstorie = false and statusFifty = true and category not in ('goals') and space = 'personal') "50%",
    count(*) filter(where statusstorie = false and statusSeventyFive = true and category not in ('goals') and space = 'personal') "75%",
    count(*) filter(where statusstorie = false and statusOneHundred = true and category not in ('goals') and space = 'personal') "100%"
from goals 

注意:如果可能的话,我想将该函数复制到 MySQL

CREATE FUNCTION filter()

标签: mysqlpostgresqlfunctionfiltercase-when

解决方案


这对你有用吗?

select
    sum(CASE WHEN statusstorie = false and statusTwentyFive = true and category not in ('goals') and space = 'personal' THEN 1 ELSE 0 END) as "25%",
    sum(CASE WHEN statusstorie = false and statusFifty = true and category not in ('goals') and space = 'personal' THEN 1 ELSE 0 END) as "50%",
    sum(CASE WHEN statusstorie = false and statusSeventyFive = true and category not in ('goals') and space = 'personal' THEN 1 ELSE 0 END) as "75%",
    sum(CASE WHEN statusstorie = false and statusOneHundred = true and category not in ('goals') and space = 'personal' THEN 1 ELSE 0 END) as "100%"
from goals 

推荐阅读