Mysql 如何在WHERE子句中使用计算列?
我试图计算出我数据库中某些产品的总容量,我试图列出所有容量为3000或更多的产品 因此,我进行如下查询:Mysql 如何在WHERE子句中使用计算列?,mysql,Mysql,我试图计算出我数据库中某些产品的总容量,我试图列出所有容量为3000或更多的产品 因此,我进行如下查询: SELECT ( products_width * products_height * products_length ) AS total_volume FROM `price` where total_volume > 3000 但是我收到一个错误,指出where子句中不存在“总体积”列,您能告诉我如何解决这个问题吗?试试: SELECT ( products_width * p
SELECT (
products_width * products_height * products_length
) AS total_volume
FROM `price`
where total_volume > 3000
但是我收到一个错误,指出where子句中不存在“总体积”列,您能告诉我如何解决这个问题吗?试试:
SELECT (
products_width * products_height * products_length
) AS total_volume
FROM `price`
having total_volume > 3000
或
为了便于解释,您不能直接在where子句中使用total\u volume
,因为它是在查询的选择部分创建的<在这种情况下,可以使用code>having,也可以使用子查询
SELECT (
products_width * products_height * products_length
) AS total_volume
FROM `price`
HAVING total_volume >= 3000
也就是说,只需将WHERE改为have即可。您需要在WHERE中重复该公式:
SELECT (products_width * products_height * products_length) AS total_volume
FROM `price`
where (products_width * products_height * products_length) > 3000
我喜欢这个解决方案,非常干净,谢谢!如果公式可能发生更改,这将导致可维护性问题,使用
have
或子查询是解决此问题的更好方法。这不会给出想要的结果。是的,当然你是对的。。。不是“分组”,而是“拥有”。。。我的头在哪里!它现在已经更新了,但是和你的答案一样,Krtek。
SELECT (products_width * products_height * products_length) AS total_volume
FROM `price`
where (products_width * products_height * products_length) > 3000