繁体   English   中英

MySql:从另一个表插入表SELECT中,其中first_table ID = another_table.id

[英]MySql: insert into table SELECT from another table where first_table ID =another_table.id

我有这个起始表:

+----+-------------+---------+
| id | id_customer | balance |
+----+-------------+---------+
|  1 |         123 | NULL    |
|  2 |         124 | NULL    |
|  3 |         125 | NULL    |
|  4 |         126 | NULL    |
+----+-------------+---------+

我需要用另一个表中的SELECT SUM填充该balance列,其中第一个表中的id_customer =第二个表中的id_customer。

+----+-------------+--------+------------+
| id | id_customer | amount | date_trans |
+----+-------------+--------+------------+
|  1 |         123 |    100 | 2018-01-01 |
|  2 |         123 |    -10 | 2018-01-04 |
|  3 |         125 |     70 | 2018-01-01 |
|  4 |         124 |     10 | 1994-05-04 |
|  5 |         124 |     20 | 2018-01-01 |
|  6 |         126 |     10 | 2018-01-16 |
|  7 |         126 |     50 | 2018-04-16 |
+----+-------------+--------+------------+

第二个表的条件是“ 2010-01-01”和“ 2018-02-28”之间的date_trans的总和(金额)。

因此,在我的示例中,我需要最终的第一个表为:

+----+-------------+---------+
| id | id_customer | balance |
+----+-------------+---------+
|  1 |         123 | 90      |
|  2 |         124 | 20      |
|  3 |         125 | 70      |
|  4 |         126 | 10      |
+----+-------------+---------+

id 2仅20,因为id 4处的10 $交易超出范围。 id 4仅10,因为id 7的50 $交易超出范围。

这是从second_table中提取SUM的(简单)查询。

SELECT id_customer , SUM(balance) AS saldo FROM second_table
WHERE date_trans BETWEEN '2010-01-01 00:00:00' AND '2018-02-28 23:59:59'
GROUP BY id_customer

通过该查询,我还有几个ID(〜99400)对我来说并不有趣,因此我需要执行类似的伪代码:

INSERT INTO first_table ( balance ) 
SELECT saldo FROM
( SELECT id_customer , SUM(balance) AS saldo FROM second_table
    WHERE date_trans BETWEEN '2010-01-01 00:00:00' AND '2018-02-28 23:59:59'
    GROUP BY id_customer )
WHERE first_table.id_customer = second_table.id_customer

我不能使用“ IN”,因为第二个表大约有100.000行。 起始桌约600张。

只需在UPDATE语句的SET子句中使用一个(相关的)子查询即可:

UPDATE first_table t1
SET t1.balance = (
  SELECT SUM(t2.amount)
  FROM second_table t2
  WHERE t2.date_trans BETWEEN '2010-01-01 00:00:00' AND '2018-02-28 23:59:59'
    AND t2.id_customer = t1.id_customer
);

演示: http : //sqlfiddle.com/#!9/9e6aa2/1

尝试这个

update first_table 
set balance = sum(B.Balance)
from first_table A
inner join second_table B on (A.id_customer = B.id_customer
                              and (B.date_trans BETWEEN '2010-01-01 00:00:00' AND '2018-02-28 23:59:59'))
group by B.id_customer 

尝试这个:

UPDATE first_table A JOIN (SELECT id_customer, SUM(amount) total_amount  
                           FROM second_table
                           WHERE date_trans BETWEEN '2010-01-01 00:00:00'
                                                AND '2018-02-28 23:59:59'
                           GROUP BY id_customer) B 
ON A.id_customer=B.id_customer
SET A.balance=B.total_amount;

看到它在SQL Fiddle上运行。

有关见解,请参见MySQL UPDATE JOIN教程。

①如果本文未解决您的问题,请点击查看与本文相关的问题
②如果本文未解决您的问题,请向程序员专用AI小助手提问
暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM