2014-10-05 107 views
1

我想知道是否有办法将查询保存为变量并稍后使用,而不是处理内联子查询。有没有办法在SQLite3中做到这一点?如何在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

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

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 
+0

谢谢!它在插入语句结尾处使用分号除外。我做了一个编辑来清除它。 :) – currysensei 2014-10-05 19:32:41