How to check if a value already exists to avoid duplicates?

前端 未结 17 1046
小鲜肉
小鲜肉 2020-12-02 23:13

I\'ve got a table of URLs and I don\'t want any duplicate URLs. How do I check to see if a given URL is already in the table using PHP/MySQL?

17条回答
  •  暖寄归人
    2020-12-02 23:31

    i don't know the syntax for MySQL, but all you need to do is wrap your INSERT with IF statement that will query the table and see if the record with given url EXISTS, if it exists - don't insert a new record.

    if MSSQL you can do this:

    IF NOT EXISTS (SELECT 1 FROM YOURTABLE WHERE URL = 'URL')
    INSERT INTO YOURTABLE (...) VALUES (...)
    

提交回复
热议问题