如何使用插入查询,其中一个值是子查询的结果


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

我有两个表,例如表1和表2。以下示例字段如下所示:

表1:ID,金额
表2:ID,total_amount

我想使用插入查询向我的表2插入一个值。但问题是我希望我的插入查询的一个值是插入语句中子查询的结果。例如:

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

我知道上面的查询是错误的。如何执行可以插入到表2的查询其中,要插入到 table2 的值之一是从我的 table1 生成的查询,如上所示?

谁能帮我解决这个问题?谢谢

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

尝试类似操作:

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

试试这个

 $sql=mysql_fetch_assoc(mysql_query("SELECT SUM(amount) as tot from table1 WHERE id=1")
 INSERT INTO table2(id, total_amount) VALUES(111,$sql['tot']);