I have two tables t1 and t2 and I want to get data from t1 and insert into the t2.
t1
id amount cId nameC open
10L 100000 CL-J Jon 0
10L 100000 CL-J Jon 0
10L 100000 CL-A Alina 0
10L 100000 CL-A Alina 0
10L 100000 CL-H Harry 0
t2
cId nameC balance
CL-J Jon 5000
CL-A Alina 10000
CL-H Harry 4000
CL-M Mia 0
I want Result like this After insert data in t1
id amount cId nameC open
10L 100000 CL-J Jon 0
10L 100000 CL-J Jon 0
10L 100000 CL-A Alina 0
10L 100000 CL-A Alina 0
10L 100000 CL-H Harry 0
10L 100000 CL-J Jon 5000
10L 100000 CL-A Alina 10000
10L 100000 CL-H Harry 4000
You can move data from one table to another by using INSERT INTO .. SELECT
:
INSERT INTO T1(id, amount, cId, nameC, open)
SELECT '10L'
, 100000
, cId
, nameC
, open
FROM t2
WHERE NOT EXISTS (SELECT 1
FROM t1
WHERE t1.id = '10L'
AND t1.Amount = 100000
AND t1.cId = t2.cId
AND t1.nameC = t2.nameC
AND t1.open = t2.open)
AND ( t2.NameC <> 'Alina' AND t2.cId <> 'CL-M' AND t2.open <> 0)
AND ( t2.NameC <> 'John Snow' AND t2.cId <> 'CL-JS' AND t2.open <> 0)
AND ... -- other conditions here
Ideally you'd have a WHERE
condition which filters the data how you need it and also makes sure that you don't accidentally insert the same data twice, by using a NOT EXISTS
statement.
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.