Fred
Fred

Reputation: 121

Android sqlite INSERT multiple rows with same constant

I'm trying to execute the following sqlite command to copy some values in a table:

INSERT OR IGNORE INTO table (column1, column2) VALUES((SELECT DISTINCT column1 FROM table WHERE column2 = '2'), 1);

What I want is for the 1 to be used for all the SELECTed rows, but it's only used once. How do I fix that?

I was thinking using a temp table with the default value to what I want. I know temp tables are used for complex queries, but it feels like this isn't that complicated and there could be an easier solution to this.

Example: I want the following table...

column1  column2
1        2
4        2
5        3
4        4

...to become the following table...

column1  column2
1        2
4        2
5        3
4        4
1        1
4        1

Upvotes: 0

Views: 543

Answers (1)

laalto
laalto

Reputation: 152817

Looks like you want to put the fixed column2 value in the select:

INSERT OR IGNORE INTO table (column1, column2) SELECT DISTINCT column1, 1 FROM table WHERE column2 = '2';

Also note the absence of VALUES.

Upvotes: 2

Related Questions