简体   繁体   English

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

[英]How can I define a query as a variable in sqlite3?

I was wondering if there was a way to save a query as a variable and use it later on, instead of dealing with inline subqueries. 我想知道是否存在一种将查询保存为变量并在以后使用的方法,而不是处理内联子查询。 Is there a way to do this in SQLite3? 有没有办法在SQLite3中做到这一点?

An example query of mine would be: 我的示例查询为:

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

I would like to define the subquery f as a variable and then use it in this fashion, if possible: 我想将子查询f定义为变量,然后在可能的情况下以这种方式使用它:

select Name
from f
where f.c = 1;

You can create in memory temp table and store the result of subquery and can use it later 您可以在内存临时表中创建并存储子查询的结果,以便以后使用

  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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM