Q:

Mysql Where Calculate

SELECT id as bid,sku,product_name,
       (SELECT SUM(piece) FROM sales WHERE uid = bid) as tsa,
       (SELECT SUM(piece) FROM buying WHERE uid = bid) as taa 
FROM product WHERE ?

I wanna where taa - tsa if 0 show query Help please I need subtraction 
I'm using WHERE taa-tsa = 0 but dont work
I solved with having thanks

A:

If you want to filter the output of a SELECT query based on the contents of an aggregate value, then you need to specify the filter in a HAVING clause (there WHERE clause filters the input rows).
Hence either:
SELECT id as bid,sku,product_name,
   (SELECT SUM(piece) FROM sales WHERE uid = bid) as tsa,
   (SELECT SUM(piece) FROM buying WHERE uid = bid) as taa 
FROM product
HAVING tsa=taa

or
SELECT ilv.*
FROM (
   SELECT id as bid,sku,product_name,
   (SELECT SUM(piece) FROM sales WHERE uid = bid) as tsa,
   (SELECT SUM(piece) FROM buying WHERE uid = bid) as taa 
   FROM product
) as ilv
WHERE tsa=taa

Your query is very badly constructed. Its impossible to tell what the query should look like without knowing a lot more about the structure of the data, the database, and the usage of the query.

