我想用一条命令插入多条记录,我可以像这样添加它们:
INSERT INTO MYTABLE (COL1,COL2,COL3) VALUES ("3","SHP","43-23"),("4", "KL", "54")我可以添加多条记录,但现在我只想在该表中不存在该记录的情况下插入。表是动态创建的,我不允许添加任何唯一的列,所以我尝试在不存在的情况下插入:
INSERT INTO b_branches (
branch_name, branch_town, account_range_lower, account_range_upper
) SELECT
'test BN' branch_name,
'test BT' branch_town,
'test ARL' account_range_lower,
'test ARU' account_range_upper
FROM DUAL
WHERE NOT EXISTS (
SELECT 1
FROM `b_branches`
WHERE
branch_name = 'test BN' AND branch_town = 'test BT' AND account_range_lower = 'test ARL'
LIMIT 1 -- will stop mysql to stop searching after first match
)此代码可以工作,但通过此代码,我一次只能添加一条记录,但我希望在一条语句中添加所有记录。有没有办法做到这一点?
发布于 2014-03-18 05:20:39
你可以随时使用触发器。但据我所知,唯一的方法是将提交转移到stop an within a trigger is to cause a ,这将回滚整个提交。
https://stackoverflow.com/questions/22465215
复制相似问题