mysql 总和返回错误值
mysql sum returning wrong value
你好所以我在下面的查询中遇到了这个问题,在 wp_wti_like_post
中我有类似 post_id
和 value
列的行数作为 1
或-1
.
所以假设对于 ID 为 727
的 post 我只有一行,所以 sum
应该是 1
,但不知道为什么 return 4
并且当有 two
行 727
时它执行 return 8
查询如下:
SELECT wp_posts. * ,
SUM( wp_wti_like_post.value ) -4 AS total_sum,
wp_wti_like_post.post_id
FROM wp_posts
INNER JOIN wp_term_relationships ON ( wp_posts.ID =
wp_term_relationships.object_id )
INNER JOIN wp_term_taxonomy ON ( wp_term_relationships.term_taxonomy_id =
wp_term_taxonomy.term_taxonomy_id )
JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )
LEFT JOIN wp_wti_like_post ON ( wp_posts.ID = wp_wti_like_post.post_id )
WHERE wp_term_taxonomy.taxonomy = 'category'
AND wp_term_taxonomy.term_id IN ('$c_cid')
AND wp_posts.post_type = 'post'
AND (wp_posts.post_status = 'publish')
GROUP BY wp_posts.ID
HAVING SUM( wp_wti_like_post.value ) > $min_like
ORDER BY wp_posts.post_date DESC
您在许多 table 之间使用连接,它们中的任何一个都有可能与 post_id
有很多关联,因此您的 sum is incorrect
我建议您使用 sub select 为您点赞 table 并在子 select 中计算总和,然后加入您的主查询
SELECT
p.*, COALESCE(l.sum_like,0) AS total_sum,
l.post_id
FROM
wp_posts p
INNER JOIN wp_term_relationships ttr
ON (p.ID = ttr.object_id)
INNER JOIN wp_term_taxonomy tt
ON (ttr.term_taxonomy_id = tt.term_taxonomy_id)
LEFT JOIN (
SELECT post_id ,SUM(`value`) sum_like
FROM wp_wti_like_post
GROUP BY post_id
) l ON (p.ID = l.post_id)
WHERE tt.taxonomy = 'category'
AND tt.term_id IN ('21')
AND p.post_type = 'post'
AND (p.post_status = 'publish')
HAVING total_sum > 2
ORDER BY p.post_date DESC
另请注意,我已删除 wp_postmeta
连接,因为它未在您的 selection 标准中使用,并且在您的过滤标准中也未使用 wp_postmeta
为每个 [=22= 存储不同的属性] 所以我想这 table 产生了更多的行,这就是计算错误总和的原因。
Removed -4 from query
你好所以我在下面的查询中遇到了这个问题,在 wp_wti_like_post
中我有类似 post_id
和 value
列的行数作为 1
或-1
.
所以假设对于 ID 为 727
的 post 我只有一行,所以 sum
应该是 1
,但不知道为什么 return 4
并且当有 two
行 727
时它执行 return 8
查询如下:
SELECT wp_posts. * ,
SUM( wp_wti_like_post.value ) -4 AS total_sum,
wp_wti_like_post.post_id
FROM wp_posts
INNER JOIN wp_term_relationships ON ( wp_posts.ID =
wp_term_relationships.object_id )
INNER JOIN wp_term_taxonomy ON ( wp_term_relationships.term_taxonomy_id =
wp_term_taxonomy.term_taxonomy_id )
JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )
LEFT JOIN wp_wti_like_post ON ( wp_posts.ID = wp_wti_like_post.post_id )
WHERE wp_term_taxonomy.taxonomy = 'category'
AND wp_term_taxonomy.term_id IN ('$c_cid')
AND wp_posts.post_type = 'post'
AND (wp_posts.post_status = 'publish')
GROUP BY wp_posts.ID
HAVING SUM( wp_wti_like_post.value ) > $min_like
ORDER BY wp_posts.post_date DESC
您在许多 table 之间使用连接,它们中的任何一个都有可能与 post_id
有很多关联,因此您的 sum is incorrect
我建议您使用 sub select 为您点赞 table 并在子 select 中计算总和,然后加入您的主查询
SELECT
p.*, COALESCE(l.sum_like,0) AS total_sum,
l.post_id
FROM
wp_posts p
INNER JOIN wp_term_relationships ttr
ON (p.ID = ttr.object_id)
INNER JOIN wp_term_taxonomy tt
ON (ttr.term_taxonomy_id = tt.term_taxonomy_id)
LEFT JOIN (
SELECT post_id ,SUM(`value`) sum_like
FROM wp_wti_like_post
GROUP BY post_id
) l ON (p.ID = l.post_id)
WHERE tt.taxonomy = 'category'
AND tt.term_id IN ('21')
AND p.post_type = 'post'
AND (p.post_status = 'publish')
HAVING total_sum > 2
ORDER BY p.post_date DESC
另请注意,我已删除 wp_postmeta
连接,因为它未在您的 selection 标准中使用,并且在您的过滤标准中也未使用 wp_postmeta
为每个 [=22= 存储不同的属性] 所以我想这 table 产生了更多的行,这就是计算错误总和的原因。
Removed -4 from query