简体   繁体   English

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

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

I have this starting table: 我有这个起始表:

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

I need to populate that balance column with a SELECT SUM from another table, where id_customer in first = id_customer in second one. 我需要用另一个表中的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 |
+----+-------------+--------+------------+

Condition of second table is SUM (amount) WHERE date_trans BETWEEN '2010-01-01' AND '2018-02-28'. 第二个表的条件是“ 2010-01-01”和“ 2018-02-28”之间的date_trans的总和(金额)。

So, in my example, I need final first table as: 因此,在我的示例中,我需要最终的第一个表为:

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

id 2 only 20 cause the 10$ transaction at id 4 is out of range. id 2仅20,因为id 4处的10 $交易超出范围。 id 4 only 10 cause the 50$ transaction at id 7 is out of range. id 4仅10,因为id 7的50 $交易超出范围。

This is the (simple) query to extract from second_table the SUM. 这是从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

WIth this query I have also several IDs (~99400) that are not interesting for me, so I need to do a similar psuedocode: 通过该查询,我还有几个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

I cannot use "IN" because second table are about 100.000 rows. 我不能使用“ IN”,因为第二个表大约有100.000行。 Starting table are about 600. 起始桌约600张。

Just use a (correlated) subquery in the SET clause of the UPDATE statement: 只需在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
);

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

try this 尝试这个

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 

Try this: 尝试这个:

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;

See it run on SQL Fiddle . 看到它在SQL Fiddle上运行。

See MySQL UPDATE JOIN tutorials for insights. 有关见解,请参见MySQL UPDATE JOIN教程。

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

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