1

我有以下 SQL 语句来计算基于 3 个 Wordpress 表的几个字段。我需要的是计算下面的语句中不起作用的SUM和。AVG

我只需要在meta_key = image. 这同样适用于AVG公式,即 avg all meta_values when meta_key=odd

任何帮助深表感谢!

SELECT (user_nicename) AS user,
       SELECT SUM(IF('meta_key' = 'image', 'meta_value' = '3279', 1,0)) AS won,
       SELECT SUM(IF('meta_key' = 'image', 'meta_value' = '3280', 1,0)) AS lost,
       COUNT('meta_key'='odd') AS total,
       FORMAT (AVG('meta_key' = 'odd', 'meta_value'),1) AS 'average odd',
       FORMAT (AVG ('meta_key' = 'bet', 'meta_value'),1) AS 'average bet',
       SUM('meta_key' = 'roi', 'meta_value') AS ROI
FROM wp_postmeta pm, wp_posts p
INNER JOIN wp_posts p ON pm.post_id = p.ID 
INNER JOIN wp_users u ON p.post_author = u.ID
WHERE Month(data) = MONTH(CURRENT_DATE)
GROUP BY (user_nicename)
ORDER BY ROI DESC
4

1 回答 1

1

在 mysql 中可能有一些方法可以简化这一点。您可能需要根据 meta_value 的类型进行转换,但要对数据子集进行平均,请使用 null 忽略:

AVG(Case When meta_key = 'odd' Then meta_value else Null End)
于 2012-11-06T19:10:49.660 回答