SUM() 函数 + 派生 table + 全部嵌套
SUM() function + derived table + nested all in one
有人可以帮我解释一下这个例子中 SUM 函数的位置吗?我指的 count
不是函数,而是我的 table 上的列名。我正在尝试对返回的列数进行字面计数
SELECT count, post_id
, DeliveryDate
, DeliveryType
FROM ( SELECT count, post_id
, MAX(CASE WHEN meta_key = 'value_1' THEN meta_value ELSE NULL END) as DeliveryDate
, MAX(CASE WHEN meta_key = 'value_2' THEN meta_value ELSE NULL END) as DeliveryType
FROM wp_postmeta
GROUP
BY post_id
) AS derived_table
WHERE DeliveryDate >= CURRENT_DATE
AND DeliveryType = 'delivery'
我的感觉是你想要这样的东西:
SELECT
post_id,
COUNT(*) AS cnt,
MAX(CASE WHEN meta_key = 'value_1' THEN meta_value END) AS DeliveryDate,
MAX(CASE WHEN meta_key = 'value_2' THEN meta_value END) AS DeliveryType
FROM wp_postmeta
GROUP BY
post_id
HAVING
DeliveryDate >= CURRENT_DATE AND
DeliveryType = 'delivery';
我们可以代替子查询然后在外部 WHERE
子句中检查交货日期和类型,而是只进行一次聚合并在 HAVING
子句中断言您的要求,使用别名较早定义。
有人可以帮我解释一下这个例子中 SUM 函数的位置吗?我指的 count
不是函数,而是我的 table 上的列名。我正在尝试对返回的列数进行字面计数
SELECT count, post_id
, DeliveryDate
, DeliveryType
FROM ( SELECT count, post_id
, MAX(CASE WHEN meta_key = 'value_1' THEN meta_value ELSE NULL END) as DeliveryDate
, MAX(CASE WHEN meta_key = 'value_2' THEN meta_value ELSE NULL END) as DeliveryType
FROM wp_postmeta
GROUP
BY post_id
) AS derived_table
WHERE DeliveryDate >= CURRENT_DATE
AND DeliveryType = 'delivery'
我的感觉是你想要这样的东西:
SELECT
post_id,
COUNT(*) AS cnt,
MAX(CASE WHEN meta_key = 'value_1' THEN meta_value END) AS DeliveryDate,
MAX(CASE WHEN meta_key = 'value_2' THEN meta_value END) AS DeliveryType
FROM wp_postmeta
GROUP BY
post_id
HAVING
DeliveryDate >= CURRENT_DATE AND
DeliveryType = 'delivery';
我们可以代替子查询然后在外部 WHERE
子句中检查交货日期和类型,而是只进行一次聚合并在 HAVING
子句中断言您的要求,使用别名较早定义。