Mysql在哪里计算

时间:2015-12-07 12:11:09

标签: php mysql

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 ?

我想在taa - tsa if 0显示查询帮助,我需要减法

我正在使用WHERE taa-tsa = 0但是没有工作

感谢我解决了

2 个答案:

答案 0 :(得分:0)

试试这个:

SELECT * 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 vw

WHERE (vw.taa - vw.tsa) = 0

答案 1 :(得分:0)

如果要根据聚合值的内容过滤SELECT查询的输出,则需要在HAVING子句中指定过滤器(WHERE子句过滤输入行)。

因此:

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

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

您的查询结构非常糟糕。如果不了解有关数据结构,数据库和查询用法的更多内容,就无法确定查询应该是什么样子。