How to use insert query where one of the value is a result of a subquery

Larcy

I have two table such as table1 and table2. Below shows the following sample fields:

table1 : id, amount
table2 : id, total_amount

I want to insert a value to my table2 using an insert query. But the problem is I want one of the values of my insert query to be the result of a subquery inside an insert statement. For example:

INSERT INTO table2(id, total_amount) VALUES(111, (SELECT SUM(amount) from table1 WHERE id=1));

I knew that the above query is wrong. How can I do the query that can be able to insert to table2 where one of the value to be inserted to table2 is the resulting query from my table1 as shown above?

Can anyone help me with this? Thanks

juergen d
INSERT INTO table2(id, total_amount) 
SELECT 111, SUM(amount) from table1 WHERE id=1

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How i can use result of subquery in this query?

php mysql use subquery result in WHERE clause in the same query

SQL insert use subquery result in another subquery

How to use the result of a subquery

How to re-use the result of a subquery in the main query, with Postgres

How do I insert multiple rows with a subquery as one of the value?

Mysql query - where clause with result of subquery

How to use WHERE in a one-to-many relationship without a subquery

How to default a value in subquery result?

How to use a subquery to filter a sqlalchemy query on a one to many relationship?

Mysql - use result of another query inside subquery

Query show result from relations where one of the value is 0

Use one select query instead of query + subquery

How to use result of first query in second query where condition?

How can I insert query result value into a constant string?

How to use a string from mysqli_result in an insert query?

Use value of primary set in where of subquery

How to use column' value in subquery?

Subquery result in the WHERE Clause

SQL subquery result in WHERE

How can I use a subquery on my query results and then ORDER BY a calculated result for each row?

How to Separate Result Set of MySql Query By Value of WHERE clause?

How to optimize SQL query with WHERE IN subquery

How to use the result "as" in "where" without using the second nested query

Insert from JSON TABLE, how to add subquery to fill one column with value outside JSON

How to Insert into … Values … Where Value= is equal to given by user? Complex query

How do I use the SQLAlchemy ORM to do an insert with a subquery (moving data from one table to another)

MySQL count across multiple columns, with subquery, with one result query

Insert result from one insert query into another insert