如何在计算列上使用WHERE?

Dav*_*ein 9 mysql

我试图在两个表中为一些完整性编写一些查询.查询是这样的

SELECT if( o.is_discounted !=1, o.item_cost, o.discounted_item_cost ) AS order_item_total,
SUM( oi.quantity * oi.price ) AS item_total
FROM orders o
INNER JOIN order_items oi ON oi.order_id = o.id
WHERE order_item_total != item_total
GROUP BY o.id
Run Code Online (Sandbox Code Playgroud)

我过去肯定会在这些列中使用别名,所以我不确定为什么在这种情况下它告诉我order_item_total不是列.

use*_*827 8

在聚合列上使用.

SELECT if(o.is_discounted != 1, o.item_cost, o.discounted_item_cost) order_item_total,
  SUM(oi.quantity * oi.price) item_total
FROM orders o
INNER JOIN order_items oi ON oi.order_id = o.id
GROUP BY o.id
HAVING order_item_total != item_total
Run Code Online (Sandbox Code Playgroud)