使用 2 个其他别名的数学结果创建别名

Creating an alias using math result of 2 other aliases

我正在尝试减去 2 个别名以创建另一个别名,但出现 "unknown column" 错误。

这是我的 SQL:

select o.id, o.name,
     (select sum(l.source_expense)
        from `assignments` as a
        left join `leads` as l on (l.id = a.lead_id)
        where a.{$this->sql_column}=o.id
        and l.date_created between {$this->date_from} and {$this->date_to}
        and find_in_set(l.vertical_id, '".implode(',', $this->app_user->verticals)."')
     ) as `expense`,
     (select sum(a.buyer_revenue)
        from `assignments` as a
        left join `leads` as l on (l.id = a.lead_id)
        where a.refunded=0
        and a.{$this->sql_column}=o.id
        and l.date_created between {$this->date_from} and {$this->date_to}
        and find_in_set(l.vertical_id, '".implode(',', $this->app_user->verticals)."')
     ) as `revenue`,
     `revenue` - `expense` as `profit`
     from {$this->sql_table} as o

基本上,我想通过从 expense 中减去 revenue 来创建一个 profit 别名。原因是我正在使用数据表并希望该列是可排序的。我已经知道我可以使用 PHP.

轻松做到这一点

我怎样才能做到这一点?

编辑 - 我已经尝试了下面的答案,但在 PHPStorm 中收到 "Each derived table should have alias" 错误,在尝试 运行 查询时收到语法错误。

这是新查询:

select t.id, t.name, t.expense, t.revenue, t.revenue - t.expense as profit
from(select o.id, o.name,
     (select sum(l.source_expense)
        from `assignments` as a
        left join `leads` as l on (l.id = a.lead_id)
        where a.{$this->sql_column}=o.id
        and l.date_created between {$this->date_from} and {$this->date_to}
        and find_in_set(l.vertical_id, '".implode(',', $this->app_user->verticals)."')
     ) as `expense`,
     (select sum(a.buyer_revenue)
        from `assignments` as a
        left join `leads` as l on (l.id = a.lead_id)
        where a.refunded=0
        and a.{$this->sql_column}=o.id
        and l.date_created between {$this->date_from} and {$this->date_to}
        and find_in_set(l.vertical_id, '".implode(',', $this->app_user->verticals)."')
     ) as `revenue`
     from {$this->sql_table} as o
 ) as t

您需要将查询放在子查询中。

SELECT 
t.*,
t.`revenue` - t.`expense` as `profit`
FROM 
(
select o.id, o.name,
     (select sum(l.source_expense)
        from `assignments` as a
        left join `leads` as l on (l.id = a.lead_id)
        where a.{$this->sql_column}=o.id
        and l.date_created between {$this->date_from} and {$this->date_to}
        and find_in_set(l.vertical_id, '".implode(',', $this->app_user->verticals)."')
     ) as `expense`,
     (select sum(a.buyer_revenue)
        from `assignments` as a
        left join `leads` as l on (l.id = a.lead_id)
        where a.refunded=0
        and a.{$this->sql_column}=o.id
        and l.date_created between {$this->date_from} and {$this->date_to}
        and find_in_set(l.vertical_id, '".implode(',', $this->app_user->verticals)."')
     ) as `revenue`
     from {$this->sql_table} as o
) AS t

注:

您只能在 GROUP BY, ORDER BY, or HAVING 子句中使用列别名。

Standard SQL doesn't allow you to refer to a column alias in a WHERE clause. This restriction is imposed because when the WHERE code is executed, the column value may not yet be determined.

Reference

只需用另一个 select 包装它,然后别名将可用于数学计算:

SELECT t.id,o.name,t.expense,t.revenue,
       t.revenue -t.expense as `profit`
FROM (Your Query Here) t