我需要将此 sql 查询转换为 hql。
SQL查询:
SELECT COALESCE(SUM(t2.Ammount * CASE WHEN t2.idAccountDeb = c.id AND t2.idAccountDeb<>t2.idAccountCred THEN 1
WHEN t2.idAccountCred AND t2.idAccountDeb<>t2.idAccountCred THEN (-1) ELSE (0) END),0)
Hql 查询不起作用:
(select COALESCE(SUM(t2.Ammount * CASE WHEN t2.AccountDeb = c AND t2.AccountDeb<>t2.AccountCred THEN 1
WHEN t2.AccountCred AND t2.AccountDeb<>t2.AccountCred THEN (-1) ELSE (0) END),0)
AST hibernate 错误:
ERROR: <AST>: 2: 24: unexpected AST node:.
最佳答案
这是 MySQL:
SELECT COALESCE(SUM(CASE WHEN t2.idAccountDeb t2.valor * = c.id AND t2.idAccountDeb <> 1
THEN t2.idAccountCred
WHEN AND t2.idAccountCred t2.idAccountDeb <> t2.idAccountCred
THEN (-1)
ELSE (0)
END), 0)
此代码在语法上不正确。 *=
不是 MySQL 运算符(列表为 here )。第二个当
格式错误时。我可能猜测预期的逻辑是:
SELECT SUM(CASE WHEN t2.idAccountDeb = t2.valor AND t2.idAccountDeb <> 1
THEN t2.idAccountCred
WHEN t2.idAccountDeb <> t2.idAccountCred
THEN -1
ELSE 0
END)
或者类似的东西。这个逻辑应该在 MySQL 和 HQL 中都有效。
编辑:
像这样的东西应该在两个数据库中都有效:
SELECT SUM(t2.Ammount * (CASE WHEN t2.idAccountDeb = c.id AND t2.idAccountDeb = t2.idAccountCred
THEN 1
WHEN t2.idAccountDeb = c.id AND t2.idAccountDeb <> t2.idAccountCred
THEN -1
ELSE 0
END)
)
关于java - 将带有 case 子句的 Sql 转换为 Hql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25309193/