SQLite INSERT with SELECT

3.7k views Asked by At

I have a problem with INSERT Query. I want to insert some fixed values as a name but as well some calculated values. A simple example:

INSERT INTO Players 
VALUES('Name', 10.0, SELECT COUNT(*) AS Amount FROM Stack7 WHERE Name LIKE '%Name%', 1.0)

Table Players:

CREATE TABLE `Players` (
    `Name`  TEXT,
    `Points`    REAL,
    `Games` REAL,
    `Result`    REAL
)

Compiler says: near "SELECT": syntax error:

1

There are 1 answers

1
Simulant On BEST ANSWER

Put your select in Brackets:

INSERT INTO Players 
VALUES('Name', 
       10.0, 
       (SELECT COUNT(*) AS Amount FROM Stack7 WHERE Name LIKE '%Name%'), 
       1.0);

In this way the compiler knows where the one value defined by your select statement starts and where it ends. And you are able to use a comma (,) inside your brackets if needed.