How do I use WHERE on Computed Column?
mysql
Solution
Use having on aggregated columns.
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
Problem
I'm trying to get some queries written for some integrity across two tables. The query is something like this ``` 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 ``` I've definitely used aliases to such columns in the past so I'm not sure why in this case it's telling me `order_item_total` is not a column.