user13631651
user13631651

Reputation:

Update using SELECT Query MySQL in Java

I have a query that have INSERT with SELECT :-

    INSERT INTO table2 (column1, column2, column3)
    SELECT column1, column2, column3
    FROM table1
    WHERE condition;

What I need to do is insert custom value for column with the select like this :

    INSERT INTO table2 (column1, column2, column3)
    SELECT column1, column3
    FROM table1
    WHERE condition
    column2 = "DATA";

The column 2 I don't need it to get it from the another table I wont to insert it.

Upvotes: 0

Views: 40

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520898

Select a literal value:

INSERT INTO table2 (column1, column2, column3)
SELECT column1, 'DATA', column3
FROM table1
WHERE condition;

I didn't include any actual Java code, but the change you would need should be straightforward. You can either include the literal in your select, or bind a constant string value to a prepared statement.

Upvotes: 2

Related Questions