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

JT Poole

I am attempting to use SQLite to retrieve data from the main table (mutants.info) and insert data from that table into a secondary table (mutants.teams). The main table has multiple columns, but I am only interested in retrieving data from one, referred to as 'team'. My goal is to have the script retrieve data from the 'team' field/column and insert that data into the secondary table and then count the number of times a certain team name appears in the field.

I wrote the following SQLite script, but it's not populating the secondary table the way I want it to.

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"))
);

When I try to run this script, it populates the secondary table with the first 'team name' it sees in the 'team' column in the main table, but does not populate any other 'team names' that are present in the 'team' field. How can I make this script pull in the other 'team names' that appear in the 'team' column?

Table Configs are as follows:

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

The goal of the script is to retrieve the data from the team field in the main table and populate the second table with that information and then count the number of times the team name appears in the main table.

I'm having difficulty getting the right coding to retrieve this data from the first table and insert it into the second table. I'll appreciate any help that someone can offer in assisting me with this matter.

forpas

I suspect that what you want is this:

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

This query selects all the (distinct) team names from "mutants.info" and inserts them in "mutants.teams" with the number of times each team appears in "mutants.info".

I don't know why you use in your code INSERT OR REPLACE instead of just INSERT.
If there are already rows in "mutants.teams" and you want them replaced by the new rows if there is a unique constraint violation on the team's name then fine.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

insert data from one table into another using a condition mysql

How to retrieve and display data from another table

How to insert the data from one column into another table

How to insert data from one table to another in array format?

Retrieve data from one table and insert/update in another table in mysql

How to replace one column with another using data.table?

Transfer data from one table to another on insert

MySQL: How to select data from a column in another table

How to select data from a table and insert into another table?

Procedure to insert data from one column into two columns in another table

MySQL insert into column data from another table

SQL. Retrieve data from the table where data is duplicated from 1 column and sifferent from another one

insert data from one table to a single column another table with no relation

Insert Data From One Table To Another - MySQL

How to select data from one table and insert it into another table with a new column

select data from one column based on another column's condition in the same table and insert that resulting data to another table

Android SQLite: How to retrieve data from a table using the id's retrieved from other table

How to get data from another table according to a retrieved data in Firebase

MYSQL: How do select column depending on data from another table?

How to insert data from one table into another as PostgreSQL array?

Copying data from one sqlite table into another

How to select data(with one decimal type) from a table and insert into another table (with different decimal type)

How to insert data from one table to another in laravel?

How to select data from table with multiple where on the same column based on another table in SQLite

How to insert from one hive table to another using select for one column

MySql: Insert data from one table to another

insert into 2 table one linked to the second through FOREIGN KEY and take data from another table in sqlite and python

How To Insert Data With Matching Parameter Values From One Table to Another?

how to insert data from one table to another table using type in plsql