如何在WHERE子句中使用计算列?

时间:2011-03-10 19:50:40

标签: mysql

我正在试图计算出我数据库中某些产品的总体积,我正在尝试列出所有容量为3000或以上的产品....

所以我这样做一个查询:

SELECT (
products_width * products_height * products_length
) AS total_volume
FROM `price`
where total_volume > 3000

但是我得到一个错误,说明where子句中不存在“total_volume”列,你能让我知道如何解决这个问题吗?

3 个答案:

答案 0 :(得分:1)

尝试:

SELECT (
products_width * products_height * products_length
) AS total_volume
FROM `price`
having total_volume > 3000

select *
from (
    SELECT (
        products_width * products_height * products_length
    ) AS total_volume
    FROM `price`
)
where total_volume > 3000

对于解释,您不能直接在where子句中使用total_volume,因为它是在查询的选择部分中创建的。在这种情况下可以使用having,或者您可以使用子查询。

答案 1 :(得分:0)

SELECT (
products_width * products_height * products_length
) AS total_volume
FROM `price`
HAVING total_volume >= 3000

即 - 改变WHERE为HAVING,就是这样。

答案 2 :(得分:0)

您需要在WHERE中重复公式:

SELECT (products_width * products_height * products_length) AS total_volume
    FROM `price`
    where (products_width * products_height * products_length) > 3000
相关问题