我有以下查询:
INSERT IGNORE INTO `article_comments` (`comment`, `user_id`, `parent_id`, `article_id`, `date`)
VALUES ('dddddddddddddddddddddd', 5, 9999, 102, '2016-03-13 17:42:19')
parent_id 9999 失败外键原因 id 9999 不存在。所以我想当外键检查失败时忽略插入并返回 0 个受影响的行,但是这段代码显示错误
#1452 - Cannot add or update a child row: a foreign key constraint fails
最佳答案
如果你使用 php,你可以这样做:
try{
$db->exec('INSERT IGNORE INTO `article_comments` (`comment`, `user_id`, `parent_id`, `article_id`, `date`)
VALUES ('dddddddddddddddddddddd', 5, 9999, 102, '2016-03-13 17:42:19')');
}
catch($e){}
关于如果外键失败,Mysql忽略插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35973648/