简体   繁体   中英

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.

+----+-------------+--------+------------+
| 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'.

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 4 only 10 cause the 50$ transaction at id 7 is out of range.

This is the (simple) query to extract from second_table the 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:

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. Starting table are about 600.

Just use a (correlated) subquery in the SET clause of the UPDATE statement:

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

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 .

See MySQL UPDATE JOIN tutorials for insights.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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