我有张桌子看起来像这样:
id url title
1 http://afef.com/abc/def/gje/qkd hello
2 http://afe.com/?a=3&b=2&fse=3 hello
3 http://stackoverflow.com/fefw/sefac/fsg hello-world从这里开始,id是主键,auto_increment是auto_increment,url是唯一的,但是title可以复制。
从这个角度来看,当我向这个表添加新URL时,如下所示:
INSERT IGNORE INTO table (url, title)
VALUES ('http://abcd.com/affefef/sfsaa/?a=3', 'this is title')然后,如果它是新的URL插入,这并不重要。但如果它是复制的网址,它将被忽略,我不知道复制的网址的id是什么。
不能更改id。
当我用一个查询插入复制的id时,有什么解决方案需要知道吗?
发布于 2016-11-15 05:36:41
有条件地检查将有助于获得重复ID。
检查此代码:
mysql.query("SELECT id FROM table WHERE url = 'http://abcd.com/affefef/sfsaa/?a=3", function(error, result, field) {
if(error) {
exist(error); //No error
} else if (result) {
if (result.length > 0){
console.log("Id exist:" + result['id']);
}else{
// write your insert statement here
}
}
});发布于 2016-11-15 06:10:58
在一个查询中,我认为这是不可能的。但是您可以使用Mysql函数"last_insert_id“检索已插入的id。检查一下,你就能看到它是否是新的了。请参阅id.aspx
您还可以查看“插入副本”。使用此语法,如果字段存在,它将更新该字段,如果找不到密钥,则插入新字段。
https://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html
https://stackoverflow.com/questions/40602584
复制相似问题