我有一个看起来像这样的表:
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,url
是唯一的,但是title
可以重复。
从这个角度来看,当我向该表添加新的 URL 时,例如:
INSERT IGNORE INTO table (url, title)
VALUES ('http://abcd.com/affefef/sfsaa/?a=3', 'this is title')
那么,如果插入的是新的URL,就没有关系了。但如果是重复的 URL,它将被忽略,我无法知道重复的 URL 的 id
是什么。
不得更改id
。
当我用一个查询插入重复的 URL 时,是否有任何解决方案可以知道 id
?
最佳答案
条件检查有助于获取重复 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
}
}
});
关于MYSQL 通过 URL 选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40602584/