51731

Syntax Error near WHERE NOT EXIST

Question:

i search in stack but None of them had reached the final answer. My query is this :

INSERT INTO user (username,frequence,autoSend) VALUES('feri2','3','1') WHERE NOT EXISTS ( SELECT * FROM user WHERE username='feri2')

When run,I receive the syntax parsing error:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE NOT EXISTS ( SELECT * FROM user WHERE username='feri2')' at line 1

I don't know how I can fix it, where is the problem?

Answer1:

You need to change the syntax slightly, try the following:

INSERT INTO user (username,frequence,autoSend) SELECT * FROM (SELECT 'feri2','3','1') AS `values` WHERE NOT EXISTS ( SELECT username FROM user WHERE username='feri2' ) LIMIT 1;

Answer2:

If you don't want duplicate users, you should probably have a UNIQUE index on the username column. If you add that, then you can simply:

INSERT IGNORE INTO user (username,frequence,autoSend) VALUES('feri2','3','1')

If there is already a user 'feri2', then it will do nothing.

Recommend

  • jQuery append function not working in Internet Explorer 8
  • how do you “Press ENTER to continue, anything else to quit” in C++
  • Why is this query not working in Sybase?
  • Parsing xml with inner nodes
  • Biztalk message agnostic orchestration
  • python argparse add_argument_group required
  • How to invoke Click using HTML AGILITY PACK
  • How do CL commands build their exact parameter lists?
  • Can I plot times when they're given as UNIX timestamps with added milliseconds?
  • Can't connect with PDO using ssl but mysqli with ssl works
  • C++ how to get substring after get position of its index
  • Deserialize Dictionary
  • How to get the sql state from libpq?
  • how to read a file in prolog?
  • Getting syntax error in mysql-php. You have an error in your SQL syntax;
  • read values from form post in jquery or javascript
  • ZipList with Scalaz
  • Alamofire and Reachability.swift not working on xCode8-beta5
  • apply a javascript function to draggable copy
  • Consuming a WCF service in a Java Client using wsHttpBinding
  • Exception gevent.hub.LoopExit: LoopExit('This operation would block forever',)
  • Regex to match a string not followed by anything
  • Question about instantiating object
  • Parse a date string in a specific locale (not timezone!)
  • Groovy: Unexpected token “:”
  • Email verification using google app script and google forms
  • How to create a file in java without a extension
  • Master page gives error
  • MySQL WHERE-condition in procedure ignored
  • ORA-29908: missing primary invocation for ancillary operator
  • How to get next/previous record number?
  • SVN: Merging two branches together
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • Benchmarking RAM performance - UWP and C#
  • How do you join a server to an Active Directory (domain)?
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • How does Linux kernel interrupt the application?