如何在sqlite3中将查询定义为变量?

时间:2014-10-05 18:55:11

标签: sql sqlite

我想知道是否有办法将查询保存为变量并稍后使用它,而不是处理内联子查询。有没有办法在SQLite3中执行此操作?

我的一个示例查询是:

select Name
from (
    select Name, Count(*) c
    from branch
    group by Name
) f
where f.c = 1;

我想将子查询f定义为变量,然后以这种方式使用它,如果可能的话:

select Name
from f
where f.c = 1;

1 个答案:

答案 0 :(得分:1)

您可以在内存中创建临时表并存储子查询的结果,以后可以使用它

  CREATE TEMP TABLE f(Name TEXT , NameCount INTEGER);

  INSERT into f 
  select Name, Count(*) c
  from branch
  group by Name;

  DROP TABLE if exists f; -- to clean up the temp table