gpt4 book ai didi

mysql - 对如何构建我的 SQL 查询感到困惑

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

我有以下查询:

SELECT 
`people`.`surename`,
calcWeight(`people`.`surname`,'kiera',6)
as `weight`
FROM
`people`
LEFT JOIN
`dogs`
ON
`people`.`id` = `dogs`.`owner`
ORDER BY
`weight` DESC

问题是我需要删除所有 weight 的结果,我正在计算的值是 0。我认为它应该是 WHERE weight > 0 但是显然 weight 直到 where 子句之后才存在。我不知道我是要移动 weight 的计算位置还是移动 WHERE。

我不想做

 WHERE calcWeight(`people`.`surname`,'kiera',6) > 0

因为 calcWeight 需要很长时间来计算(当它发生在数千行上时)而且我不知道它是否会缓存结果并重新使用它。

任何帮助和建议都会很棒!谢谢!

最佳答案

最好的方法是使用子查询:

select t.*
from (SELECT `people`.`surename`, calcWeight(`people`.`surname`,'kiera',6) as `weight`
FROM `people` LEFT JOIN
`dogs`
ON `people`.`id` = `dogs`.`owner`
) t
where weight > 0
ORDER BY `weight` DESC

关于mysql - 对如何构建我的 SQL 查询感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12771108/

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