我可以在SQLite中重命名查询结果的列吗?

时间:2020-07-16 15:07:56

标签: sql sqlite

我有一个查询,其中返回的某些列是sum()函数的结果,该列的名称为“ sum(price)”,但我希望列名称为“ total”。有没有办法在SQLite中做到这一点?

查询:

SELECT name, sum(price) FROM trades WHERE trader_id = 7 group by symbol;

当前输出:


+------------+-------------+
|    name    | sum(shares) |
+--------------------------+
|   netflix  |       523.26|
+--------------------------+
|   tesla    |      9276.06|
+------------+-------------+

预期输出:

+------------+-------------+
|    name    |    total    |
+--------------------------+
|   netflix  |       523.26|
+--------------------------+
|   tesla    |      9276.06|
+------------+-------------+

2 个答案:

答案 0 :(得分:2)

as total确实可以满足您的需求。它会创建一个alias

SELECT name, sum(price) as total FROM trades WHERE trader_id = 7 group by symbol;

答案 1 :(得分:1)

当然,您可以使用列别名:

SELECT name, sum(price) total FROM trades WHERE trader_id = 7 group by symbol;