user7159879 user7159879 - 19 days ago 6
Node.js Question

MYSQL select by URL

I have a table looks like this:

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


from here
id
is primary key and auto_increment, and
url
is unique, but
title
can be duplicated.

From this point of view, when I add new URL to this table, like :

INSERT IGNORE INTO table (url, title)
VALUES ('http://abcd.com/affefef/sfsaa/?a=3', 'this is title')


Then, if it is new URL to insert, it doesn't matter. But if it is duplicated URL, it will be ignored and I can't know what the duplicated URL's
id
is.

The
id
must not be changed.

Is there any solutions to know
id
when I INSERT duplicated URL with one query ?

zan zan
Answer

Conditionally checking would help in getting duplicate ID

Check this code:

 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
                }
            }
        });
Comments