Select a "virtual" column calculated from other existing columns

Is it possible in sql / sqlite3 to make a choice where the column does not exist but is the computed result of the other columns?

eg. Given a table with max and current columns, I would like to make a selection based on the difference between them, so if I had:

table_data:
max|current
10|3
12|8

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

desired result:
4
7

sqlite3 grammar here http://www.sqlite.org/lang_expr.html , but I can not understand it

+3
source share
2 answers
select `max` - `current` as `result` from table_data order by `result` asc
+3
source

If it's not just a one-off, you will probably be better off looking.

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

Then you can just

 select diff from yourtable_diffs order by diff;

You can take a look at the SQL tutorial from FirstSQL J.

+1
source

Source: https://habr.com/ru/post/1788761/


All Articles