无法从子查询结果中选择计算值

时间:2018-06-27 02:36:06

标签: mysql

我的目标是从另一个表中获取一些计数,然后根据该差异计算差异和顺序,以确定效果最好和效果最低的结果。

select
    `id`,
    `subscriber_count`,
    ([SUBQUERY]) as `subscribed_count`,
    ([SUBQUERY]) as `unsubscribed_count`,
    (`subscribed_count` - `unsubscribed_count`) AS `difference`
from `mytable`
order by `difference` ASC

问题是我遇到此错误:

Error Code: 1054. Unknown column 'subscribed_count' in 'field list'

如何按差异排序并选择?

1 个答案:

答案 0 :(得分:0)

请尝试使用此代码

select
    `id`,
    `subscriber_count`,
    ([SUBQUERY]) as `subscribed_count`,
    ([SUBQUERY]) as `unsubscribed_count`,
    (([SUBQUERY]) - ([SUBQUERY])) AS `difference`
from `mytable`

问题:subscribed_count,unsubscribed_count不是字段,它们只是mysql中的别名,因此无法通过别名进行任何操作。

谢谢。 希望对您有所帮助:)