Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 将一个表中的总和添加到第二个表中_Mysql_Sql_Sql Server - Fatal编程技术网

Mysql 将一个表中的总和添加到第二个表中

Mysql 将一个表中的总和添加到第二个表中,mysql,sql,sql-server,Mysql,Sql,Sql Server,我有两张桌子 Child_id (this is temporary table) -------- 1001 ---------- 1002 ---------- 1003 ---------- 1004 ---------- 1006 - 现在我想将第二个表中两个子ID相同的总金额添加到第一个表中。 例如,此表的输出为600(加上1001100210031004) 请你帮我做这件事。你可以做: SELECT SUM(u.amount) FROM tempTable t INNER JOIN

我有两张桌子

Child_id (this is temporary table)
--------
1001
----------
1002
----------
1003
----------
1004
----------
1006
-

现在我想将第二个表中两个子ID相同的总金额添加到第一个表中。 例如,此表的输出为600(加上1001100210031004)

请你帮我做这件事。

你可以做:

SELECT SUM(u.amount)
FROM tempTable t
INNER JOIN user_details u ON t.child_id = u.child_id
这将导致两个表中具有child_id的记录的金额总和

尝试以下操作: 此查询类似于,除了异常情况下金额的IFNULL检查

SELECT 
    IFNULL(SUM(ud.amount),0)
FROM 
    user_details ud
INNER JOIN 
    temporary_table tt
ON 
    ud.child_id = tt.child_id
创建您的表

create table temporary_table (total_sum decimal(18, 2)) ;
插入数据

INSERT INTO temporary_table
SELECT SUM(u.amount)
FROM tempTable t
 JOIN user_details u ON t.child_id = u.child_id;

如果你有非常大的桌子,那么这个变体更好

SELECT SUM(u.amount)
FROM user_details u
where exists (select * from tempTable t where t.child_id = u.child_id)

标签是微软的产品,我猜你也添加了标签,这是不正确的?Thnks bro。非常感谢你给我的信
SELECT SUM(u.amount)
FROM user_details u
where exists (select * from tempTable t where t.child_id = u.child_id)