Reputation: 179
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?
Upvotes: 2
Views: 1893
Reputation: 8995
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.
Upvotes: 0
Reputation: 30809
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;
Upvotes: 3