MYSQL 通过 URL 选择

标签 mysql node.js

我有一个看起来像这样的表:

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/

相关文章:

mysql - 查找有 0 个帖子、1-5 个帖子和 >5 个帖子的用户的平均年龄

mysql - 具有多个 AND OR WHERE 条件的复杂 MySQL 查询

javascript - 获取字符串 Javascript 中的所有单词

node.js+mongodb 无法链接数据库(控制台错误)

javascript - 如何为nodeJs项目创建模板原型(prototype)

templates - 在 Jade View 中使用 javascript 代码 - if(variable) 显示未定义而不是传递

mysql - 在 MySQL 中的 JOIN 中进行排序 - 每个用户的最新消息

mysql - 如何查询带有可能未输入参数的对象列表?

node.js - 回滚 pm2 部署到特定提交

mysql - Laravel 外键约束迁移错误