是否有可能在不使用任意SQLite.Swift的情况下执行以下查询(我确实可以使用,但希望避免)?
select table1.id, sum(table1.col1*table2.col2)
from table1, table2
where table1.id=table2.id
group by table1.id我尝试过以下操作: SQL (通过asSQL())似乎是正确的,但我找不到一种方法来引用返回行中的聚合列。
let query = table1.select(id, (table1[column1]*table2[column2]).sum
.join(table2, on: table1[id] == table2[id])
.group(id)你能以某种方式给列起别名吗?
发布于 2018-08-27 19:08:44
好了,我已经找到解决方案了,只花了我两天的时间!
在SQLite.swift中为列添加别名的方法是使用表达式。表达式的名称将成为列别名。
因此,不是
let query = table1.select(id, (table1[column1]*table2[column2]).sum)
.join(table2, on: table1[id] == table2[id])
.group(id)使用:
let aggrColumn = (table1[column1]*table2[column2]).sum
let query = table1.select(id, aggrColumn)
.join(table2, on: table1[id] == table2[id])
.group(id)
let results = try db.prepare(query)
for row in results {
myAggrColumn = try row.get(aggrColumn)
}https://stackoverflow.com/questions/52025243
复制相似问题