Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2012
    Posts
    91

    Unanswered: INSERT with NOT EXISTS

    How to check if the input value to a table column already exist. I`m trying to use the phrase where not exists
    Code:
    INSERT INTO TABLE (ID) VALUES ('1') WHERE NOT EXISTS ID=1;
    but without success.
    Logically, the word is: 'IF' but there is no such query.

  2. #2
    Join Date
    Feb 2008
    Location
    Japan
    Posts
    3,483
    EXISTS and NOT EXISTS are used with Subqueries.

    Please see MySQL Reference Manual
    MySQL :: MySQL 5.1 Reference Manual :: 13.2.9.6 Subqueries with EXISTS or NOT EXISTS

  3. #3
    Join Date
    Jan 2012
    Posts
    91
    Thanks, but this query makes error from WHERE NOT EXISTS ...
    Code:
    INSERT INTO TABLE (ID) VALUES ('1') WHERE NOT EXISTS (SELECT * FROM TABLE WHERE ID=1);
    Perhaps EXISTS subquery starts only with SELECT?

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by duf View Post
    How to check if the input value to a table column already exist.
    the answer is, you don't need to check if you've defined that column as unique

    look up the INSERT ... ON DUPLICATE KEY syntax in da manual
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •