-2

How i can to make a dynamic, calculated column? Which is calculated using other columns and not stored in the database. Which can be used for data output and sorting.

Library: stephencelis/SQLite.swift (https://github.com/stephencelis/SQLite.swift)

Example:

SELECT a+b as c from test ORDER BY c ASC;

Thanks.

Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52

1 Answers1

0

With sqlite 3.31.0 and newer, you can use a generated column.

CREATE TABLE test(a INTEGER, b INTEGER,
    c INTEGER GENERATED ALWAYS AS (a + b) VIRTUAL);

Or use a view:

CREATE TABLE real_test(a INTEGER, b INTEGER);
CREATE VIEW test AS
    SELECT a, b, a + b AS c FROM real_test;
Shawn
  • 47,241
  • 3
  • 26
  • 60