SQLite:如何使用 SELECT 从一个 table 中的列检索数据以将检索到的数据插入另一个 table

SQLite: How to retrieve data from column in one table using SELECT to insert retrieved data in another table

我正在尝试使用 SQLite 从主 table (mutants.info) 检索数据并将数据从 table 插入辅助 table (mutants.teams).主 table 有多个列,但我只对从一个列中检索数据感兴趣,称为 'team'。我的目标是让脚本从 'team' field/column 检索数据并将该数据插入辅助 table,然后计算某个团队名称出现在该字段中的次数。

我编写了以下 SQLite 脚本,但它没有按照我希望的方式填充辅助 table。

INSERT OR REPLACE INTO "mutants.teams" (team,members) Values(
(SELECT team FROM "mutants.info"),
(SELECT COUNT(*) FROM "mutants.info" WHERE team = (SELECT team FROM "mutants.info"))
);

当我尝试 运行 这个脚本时,它会用它在主 table 的 'team' 列中看到的第一个 'team name' 填充辅助 table =],但不会填充 'team' 字段中存在的任何其他 'team names'。我怎样才能让这个脚本拉入 'team' 列中出现的其他 'team names'?

Table配置如下:

+-----------------------------------------------------------+
|                           main                            | 
+-----------------------------------------------------------+
|id |mutant  |powers             |team         |location    |   
+-----------------------------------------------------------+
|1  |Veto    |Psionic            |Ninjas       |China       |    
+-----------------------------------------------------------+
|2  |Wrecker |Enhanced Strength  |The Crew     |Chicago     |   
+-----------------------------------------------------------+
|3  |Atlas   |Godlike Powers     |The Gods     |Heaven      |   
+-----------------------------------------------------------+
|4  |Aria    |Sonic Energy Powers|The Crew     |Chicago     |
+-----------------------------------------------------------+
 
+-----------------------+
|       secondary       |
+-----------+-----------+
| team      | members   |
+-----------+-----------+
| The Crew  | 13        |
+-----------+-----------+
| Ninjas    | 27        |
+-----------+-----------+
| The Gods  | 127       |
+-----------+-----------+
| Chosen    | 600       |
+-----------+-----------+

脚本的目标是从主 table 中的团队字段中检索数据并用该信息填充第二个 table,然后计算团队名称出现的次数主要 table.

我很难获得正确的编码来从第一个 table 检索此数据并将其插入第二个 table。如果有人可以帮助我解决这个问题,我将不胜感激。

我怀疑你要的是这个:

INSERT INTO "mutants.teams" (team, members)
SELECT team, COUNT(*) 
FROM "mutants.info"
GROUP BY team

此查询从 "mutants.info" 中选择所有(不同的)团队名称并将它们插入 "mutants.teams" 中,每个团队在 "mutants.info" 中出现的次数。

我不知道你为什么在代码中使用 INSERT OR REPLACE 而不是 INSERT.
如果 "mutants.teams" 中已经有行并且你希望它们被新行替换,如果团队名称违反了唯一约束,那么没问题。