gpt4 book ai didi

php - CakePHP 虚拟字段是其他三个虚拟字段的总和?

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

我在 CakePHP 中有一个虚拟字段,它需要是我的用户模型中三个非常不同的 SQL 查询的 SUM。我试图通过拥有一个由 3 个其他虚拟字段组成的虚拟字段来实现这一点。

var $virtualFields = array (
'field_one' => 'select coalesce(sum(coalesce(t_a.field, 0)), 0)*10 as field_one from t_a join t_b on t_a.t_b_id = t_b.id where t_b.user_id=User.id',
'field_two' => 'select coalesce(sum(coalesce(t_c.field, 0)), 0)*2 as field_two from t_d left join (t_c) on (t_d.id=t_c.t_d_id) where t_d.user_id = User.id',
'field_three' => 'select coalesce(sum(coalesce(value, 0)), 0) as field_three from t_e where user_id=User.id',
'field_sum' => 'User.field_one+User.field_two+User.field_three'
);

这行不通。当它到达“field_sum”时,我收到错误“field_one doesn't exist”。之前问过如何组合这三个sql语句,一直没有得到满意的回答。事实证明,简单地单独运行它们并在事后对它们求和要好得多,也容易得多。在 CakePHP 的上下文中有什么方法可以做到这一点吗?

编辑

这是 cake 生成的 SQL:

SELECT 
/* Users fields */
(select coalesce(sum(coalesce(t_a.field, 0)), 0)*10 as field_one from t_a join t_b on t_a.t_b_id = t_b.id where t_b.user_id=User.id) AS `User__field_one`,
(select coalesce(sum(coalesce(t_c.field, 0)), 0)*2 as field_two from t_d left join (t_c) on (t_d.id=t_c.t_d_id) where t_d.user_id = User.id) AS `User__field_two`,
(select coalesce(sum(coalesce(value, 0)), 0) as bonus_reputation from reputation_bonuses where user_id=User.id) AS `User__field_three`, (`User`.`field_one`+`User`.`field_two`+`User`.`field_three`) AS `field_sum`,
FROM `users` AS `User`
WHERE `User`.`email` = '/* redacted */' AND `User`.`password` = '/* redacted */' LIMIT 1

看到我尝试将定义更改为 (User__field_one+User__field_two+User__field_three) 以利用它们的命名方式。运气不好。

确切的错误是:SQL 错误:字段列表中的 1054 未知列 User.field_one

最佳答案

只需删除别名:

'field_sum' => 'field_one + field_two + field_three'

关于php - CakePHP 虚拟字段是其他三个虚拟字段的总和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3861652/

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