我有两个MySQL表:
tags: id / name / value
tag_groups: id / tag_idtags.id是一个自动增量值。所有标签都是单独插入的。下面是三个INSERT INTO标记的结果:
tags
1 "manufacturer" "Samsung"
2 "manufacturer" "Sony"
3 "manufacturer" "Apple"我将每个id返回到一个数组id中,并在tag_groups中创建一个相应的插入:
tag_groups
1 1
1 2
1 3如何防止tag_groups两次插入同一组ids?
tag_groups
1 1
1 2
1 3
2 1
2 2
2 3PS:如果这是一个已知的概念/问题,我道歉。我不知道该搜索什么。
发布于 2013-12-09 11:21:07
尝试使用INSERT IGNORE而不是INSERT only。如果一条记录没有复制现有的记录,它将正常插入该记录。如果该记录是重复的,那么mysql将忽略您的insert查询,不会产生任何类型的错误。
例如:
mysql> INSERT IGNORE INTO person_tbl (last_name, first_name)
-> VALUES( 'Jay', 'Thomas');参考http://www.tutorialspoint.com/mysql/mysql-handling-duplicates.htm
https://stackoverflow.com/questions/20462476
复制相似问题