【问题标题】:doctrine sum of values depending on account source取决于账户来源的价值总和
【发布时间】:2015-09-02 11:54:19
【问题描述】:

我目前正面临一个我真的不知道如何在教义上解决的问题。

这是我当前的查询:

SELECT SUM(t.amount) FROM AccountingBundle:Entity:Transaction t WHERE t.SourceAccount = ?1 or t.TargetAccount = ?1

TargetAccount 应为正数,SourceAccount 应为负数。 我真的不想做 2 次查询,因为这个学说和 PostgreSQL 更快。

我找到了一个可能的解决方案使用

CASE WHEN t.SourceAccount = ?1 THEN ... ELSE ...

但我还没有找到一种方法来告诉教义价值是否定的。 谁能告诉我如何实现这样的目标?

【问题讨论】:

    标签: php postgresql orm doctrine-orm relational-database


    【解决方案1】:

    不会做:

    SELECT
      SUM(CASE WHEN t.SourceAccount = ?1 THEN t.ammount * -1 ELSE t.ammount)
    FROM
      AccountingBundle:Entity:Transaction t
    WHERE
      t.SourceAccount = ?1 or t.TargetAccount = ?1
    

    解决您的问题?

    编辑:这里的诀窍是将金额乘以 -1 使其成为负数

    【讨论】:

    • 我会尽快尝试的。
    • 到目前为止从未听说过 * -1。感谢您的提示
    猜你喜欢
    • 1970-01-01
    • 2016-10-15
    • 1970-01-01
    • 2021-05-01
    • 2022-01-06
    • 2021-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多