我有两个表t1和t2,我想从t1获取数据并插入到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
我想要这样的结果在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
答案 0 :(得分:0)
您可以使用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
理想情况下,您有一个WHERE
条件可以根据需要过滤数据,并确保您不会意外地使用{{1}两次插入相同的数据声明。