SQLITE,创建一个临时表,然后从中选择 [英] SQLITE, Create a temp table then select from it
问题描述
只是想知道如何创建一个临时表,然后从脚本中进一步选择它.
示例.
创建临时表 TEMP_TABLE1 AS选择身份证,SUM(L.cost)/2 作为成本,来自表 1 LJOIN Table2 C on L.ID = C.ID其中 C.name = 'mike'按 L.ID 分组选择计数(L.ID)来自表 1 L在 L.ID = TT1.ID 上加入 TEMP_TABLE1 TT1;L.ID 不在的地方 (TT1)和 Sum(L.Cost) >TT1.成本
理想情况下,我想要一个临时表,然后在脚本中稍后使用它来引用.
任何帮助都会很棒!
您只需将表格称为 根据:- 如果指定了模式名,它必须是main"、temp"或附加数据库的名称.在这种情况下,新表是在命名数据库中创建.如果TEMP"或TEMPORARY"关键字发生在CREATE"和TABLE"之间,然后创建新表在临时数据库中.同时指定模式名和TEMP 或 TEMPORARY 关键字,除非架构名称是temp".如果没有模式名称已指定且 TEMP 关键字不存在,则表在主数据库中创建. 此示例存在一些问题,禁止滥用聚合(已注释掉)以下工作. :- 问题基于列名称 just wondering how i can create a temp table and then select from it further down the script. Example. Ideally I want to have a temp table then use it later in the script to reference from. Any help would be great! You simply refer to the table as As per :- If a schema-name is specified, it must be either "main", "temp", or
the name of an attached database. In this case the new table is
created in the named database. If the "TEMP" or "TEMPORARY" keyword
occurs between the "CREATE" and "TABLE" then the new table is created
in the temp database. It is an error to specify both a schema-name and
the TEMP or TEMPORARY keyword, unless the schema-name is "temp". If no
schema name is specified and the TEMP keyword is not present then the
table is created in the main database. SQL As Understood By SQLite - CREATE TABLE The following example creates 3 tables :- :-
:-
:- The result being :-
There are a few issues with this example bar the misuse of the aggregate (commented out) the following works. :- The issues are based upon the column name being Adding the following may be suitable for getting around the misuse of aggregate :-
Adding the following to the end of the script :- results in :- 这篇关于SQLITE,创建一个临时表,然后从中选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!temp.
或
后者仅当它是唯一的表名时.
重新添加示例:-
<块引用>创建临时表 TEMP_TABLE1 AS选择身份证,SUM(L.cost)/2 作为成本,来自表 1 LJOIN Table2 C on L.ID = C.ID其中 C.name = 'mike'按 L.ID 分组选择计数(L.ID)来自表 1 L在 L.ID = TT1.ID 上加入 TEMP_TABLE1 TT1;L.ID 不在的地方 (TT1)和 Sum(L.Cost) >TT1.成本
_
.DROP TABLE IF EXISTS Table_1;如果存在表_2,则删除表;删除表如果存在 temp.temp_table1;CREATE TABLE Table_1 (ID INTEGER PRIMARY KEY, cost REAL);CREATE TABLE Table_2 (ID INTEGER PRIMARY KEY, name TEXT);INSERT INTO Table_1 (cost) VALUES (100.45),(56.78),(99.99);INSERT INTO Table_2 (name) VALUES ('mike'),('mike'),('fred');创建临时表 temp_table1 AS选择 L.ID,sum(L.cost)/2 作为成本从表_1 LJOIN Table_2 C ON L.ID = C.IDWHERE C.name = '迈克'按 L.ID 分组;选择计数(L.ID)从表_1 L加入 temp_table1 TT1 ON L.ID = TT1.[L.ID]哪里L.ID 不在 (TT1.[L.ID])-- AND Sum(L.cost) >TT1.costs --<<<<滥用聚合
L.ID
,因此必须将其括起来(此处的规则 CREATE TEMPORARY TABLE TEMP_TABLE1 AS
Select
L.ID,
SUM(L.cost)/2 as Costs,
from Table1 L
JOIN Table2 C on L.ID = C.ID
Where C.name = 'mike'
Group by L.ID
Select
Count(L.ID)
from Table1 L
JOIN TEMP_TABLE1 TT1 on L.ID = TT1.ID;
Where L.ID not in (TT1)
And Sum(L.Cost) > TT1.Costs
temp.<table>
or <table>
the latter only if it is a unique table name.
DROP TABLE IF EXISTS temp.table1;
DROP TABLE IF EXISTS table1;
DROP TABLE IF EXISTS temp_table;
CREATE TABLE table1 (columnA INTEGER,columnB INTEGER, columnC INTEGER);
INSERT INTO table1 (columnA,columnB,columnC) VALUES
(1,5,20),
(2,7,21),
(3,8,80),
(4,3,63);
CREATE TEMP TABLE table1 AS select * from table1;;
CREATE TEMPORARY TABLE temp_table AS SELECT * FROM table1;
temp.
and just the table name. SELECT 'temp_table' AS from_table,* FROM temp_table
UNION ALL
SELECT 'temp.table1' as from_table,* FROM temp.table1;
Re addition of example :-
CREATE TEMPORARY TABLE TEMP_TABLE1 AS
Select
L.ID,
SUM(L.cost)/2 as Costs,
from Table1 L
JOIN Table2 C on L.ID = C.ID
Where C.name = 'mike'
Group by L.ID
Select
Count(L.ID)
from Table1 L
JOIN TEMP_TABLE1 TT1 on L.ID = TT1.ID;
Where L.ID not in (TT1)
And Sum(L.Cost) > TT1.Costs
_
to the table names.DROP TABLE IF EXISTS Table_1;
DROP TABLE IF EXISTS Table_2;
DROP TABLE If EXISTS temp.temp_table1;
CREATE TABLE Table_1 (ID INTEGER PRIMARY KEY, cost REAL);
CREATE TABLE Table_2 (ID INTEGER PRIMARY KEY, name TEXT);
INSERT INTO Table_1 (cost) VALUES (100.45),(56.78),(99.99);
INSERT INTO Table_2 (name) VALUES ('mike'),('mike'),('fred');
CREATE TEMP TABLE temp_table1 AS
SELECT L.ID,
sum(L.cost)/2 as Costs
FROM Table_1 L
JOIN Table_2 C ON L.ID = C.ID
WHERE C.name = 'mike'
GROUP BY L.ID;
SELECT
count(L.ID)
FROM Table_1 L
JOIN temp_table1 TT1 ON L.ID = TT1.[L.ID]
WHERE
L.ID NOT IN (TT1.[L.ID])
-- AND Sum(L.cost) > TT1.costs --<<<< misuse of aggregate
L.ID
so this has to be enclosed (rules here SQL As Understood By SQLite - SQLite Keywords apply) [
and ]
have been used above.
AS
e..g SELECT
L.ID AS lid, --<<<< AS lid ADDED
SUM(L.cost)/2 as Costs, ,.......
GROUP BY L.ID
HAVING sum(L.cost) > TT1.costs
SELECT
count(L.ID), *
FROM Table_1 L
JOIN temp_table1 TT1 ON L.ID = TT1.[L.ID];
登录
关闭