简体   繁体   中英

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?

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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