我在堆栈中搜索,但他们都没有达到最终答案。 我的疑问是:
INSERT INTO user (username,frequence,autoSend) VALUES('feri2','3','1') WHERE NOT EXISTS ( SELECT * FROM user WHERE username='feri2')
运行时,我收到语法分析错误:
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
我不知道如何解决问题,问题出在哪里?
答案 0 :(得分:2)
您需要稍微更改语法,请尝试以下操作:
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;
答案 1 :(得分:0)
如果您不想要重复用户,则username
列上可能应该有一个UNIQUE索引。如果你添加它,那么你可以简单地:
INSERT IGNORE INTO user (username,frequence,autoSend)
VALUES('feri2','3','1')
如果已经有用户'feri2',那么它什么都不做。