gpt4 book ai didi

php - MySQL函数: in clause HAVING query runs twice

转载 作者:行者123 更新时间:2023-11-29 07:59:22 24 4
gpt4 key购买 nike

我编写了 MySQL 函数来检索数据库记录的总和。

SELECT id, myFunction(id) as price from myTable;

返回:

+———————+————————+
| id | price |
| 1 | 10 |
| 2 | 20 |
| 3 | 30 |
| 4 | 40 |
| 5 | 50 |
+———————+————————+

当我尝试将结果限制在 20 到 40 之间的价格时出现问题。

如果我在HAVING CLAUSE中这样做,这个函数将被执行两次。第一次是在 SELECT myFunction 中检索时,第二次是在 HAVING PRICE 附近。

示例:

SELECT id, myFunction(id) as price from myTable HAVING price BETWEEN 20 AND 40.

是否可以将此查询修改为一次性执行?由于计算复杂,所以最好不要运行两次相同的操作。

最佳答案

您可以尝试使用如下子查询“具体化”结果:

SELECT * FROM
(SELECT id, myFunction(id) AS price FROM myTable) t
WHERE price BETWEEN 20 AND 40

关于php - MySQL函数: in clause HAVING query runs twice,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24291233/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com