如何使用“case-when"在灵丹妙药的 Ecto 查询中? [英] How to use "case-when" in Ecto Queries in elixir?
问题描述
我有一个 SQL 查询,例如: I have an SQL query like : 我想为上面写相应的Ecto Query.类似于: I want to write the corresponding Ecto Query for the above. Something like: 上述案例中case-when"的类比是什么? What is the analogy to "case-when" in the above case? 如评论所说,可以使用 Like the comment said, you can use 如果你想指定表格,这对我有用: If you want to specify the table, this works for me: 这篇关于如何使用“case-when"在灵丹妙药的 Ecto 查询中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!SELECT SUM(CASE WHEN
.
SELECT SUM(CASE WHEN <table_name>.status = '2' THEN 1 ELSE 0 END) FROM <table name>
.from t in <table_name>, select: sum(...)
推荐答案
fragment/1
:fragment/1
:query = from t in <Model>, select: fragment("SUM(CASE WHEN status = ? THEN 1 ELSE 0 END)", 2)
query = from t in <Model>, select: fragment("SUM(CASE WHEN ? = ? THEN 1 ELSE 0 END)", t.status, 2)
登录
关闭