mysql关联插入

1、关联插入(直接从TableB取数据插入TableA)
INSERT INTO tableA (Xpid,Bid,Proid)
SELECT ID, Bid,Proid FROM tableB
WHERE Bid=1;

2、关联插入(直接从TableB取数据插入TableA另外加有一个固定的字段)

INSERT INTO tableA (Xpid,Bid,Proid,Time) SELECT ID, Bid,Proid,'2018-08' FROM tableB  WHERE Bid=1;

3、关联插入之前判断是否已经手动插入了
INSERT INTO tableA(Xpid,Bid,Proid,Time)
SELECT ID, Bid,Proid,'2018-08' FROM tableB as b
WHERE Bid=18 and not exists (select ID from tableA as a where a.Xpid=b.ID and Time='2018-08'  and Bid=18)

你可能感兴趣的:(mysql关联插入)