选择从其他现有列计算的“虚拟”列

时间:2011-01-30 05:00:11

标签: sql sqlite

在sql / sqlite3中是否可以执行列不存在但是其他列的计算结果的选择?

e.g。给定一个包含max和current列的表,我想对两者之间的差异进行选择,如果我有:

table_data:
max|current
10|3
12|8

select ???? from table_data order by ????? asc;

desired result:
4
7

sqlite3语法在这里http://www.sqlite.org/lang_expr.html但不能理解它

2 个答案:

答案 0 :(得分:4)

select `max` - `current` as `result` from table_data order by `result` asc

答案 1 :(得分:1)

如果这不仅仅是一次性的话,你可能会更好看一下。

create view yourtable_diffs as 
select max, current, max - current as diff from yourtable;

然后你可以

 select diff from yourtable_diffs order by diff;

您可以查看FirstSQL J中的SQL tutorial