【发布时间】:2017-10-03 13:47:02
【问题描述】:
我对两列求和的基本操作有疑问。这很简单,但它不起作用。
我得到结果 5 + 5 = 8, 3 + 7 = 7
这是查询:
select
`wp_posts`.ID ,
(select count(*) from `co_likes`
where `wp_posts`.`ID` = `co_likes`.`id_post`
and `co_likes`.`deleted_at` is null) as `like_count`,
(select count(*) from `wp_comments`
where `wp_posts`.`ID` = `wp_comments`.`comment_post_ID`) as `comment_count` ,
(`comment_count`+`like_count`) as 'total_sum'
from
`wp_posts`
where
`post_type` in ('post', 'co_post')
and `post_status` = 'publish'
order by
(comment_count+like_count) desc;
结果如下:
知道发生了什么吗?
【问题讨论】:
-
你不能使用之前在同一个查询中定义的列别名。我不确定这个查询最初是如何运行的。