Cem Cem - 4 months ago 9
MySQL Question

MySQL - Syntax error when using <> in a where statement (insert into)

i was trying to build an 'insert if' mechanism in mysql with some conditions.

The topic that i followed to build it : MySQL INSERT IF (custom if statements)

The entire code is that :

insert into qualityScore (adGroupID, keywordID, qualityScore, day, clientID)
select 222, 212, 6, '2016-07-23', 5
where ( select qualityScore from qualityScore where adGroupID=222 and keywordID = 212 and day < '2016-07-23' ) <> 5


But there is a little trouble with the below :

(select qualityScore from qualityScore where adGroupID=222 and keywordID = 212 and day < '2016-07-23')


When i execute this piece alone, it returns something like that :

QualityScore
------------
5


It's an expected result to get. But then the problem exists when i add to code the ' <> 5 ' part :

(select qualityScore from qualityScore where adGroupID=222 and keywordID = 212 and day < '2016-07-23') <> 5


It returns a MySQL 1064 syntax error code.
As far as i know, syntax seems right and i can't find the point that leads to this problem.

Any suggestions will be appreciated. Thanks.

Answer

You can't have a WHERE clause without a FROM clause. If you're just selecting literals without a table, use the dummy table DUAL.

insert into qualityScore (adGroupID, keywordID, qualityScore, day, clientID) 
select 222, 212, 6, '2016-07-23', 5
from DUAL
where ( select qualityScore from qualityScore where adGroupID=222 and keywordID = 212 and day < '2016-07-23' ) <> 5

DEMO

Comments