What I want to achieve is, If the number of rows returned by the query:
SELECT count(*) FROM `games` WHERE cateID=2 AND active=1
is EQUAL to 0, I want to run an inserting query:
INSERT INTO games(cateID ,time,users,active)
VALUES ('2', '1', '0', '1')
I tried using case like this:
SELECT CASE WHEN SELECT count(*) FROM `games` WHERE cateID=2 AND active=1)=0
THEN INSERT INTO games(cateID ,time,users,active)
VALUES ('2', '1', '0', '1')
END
But it returns error as:
#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 'SELECT count(*) FROM `games` WHERE cateID=2 AND active=1)=0
THEN INSERT INTO' at line 1
Note: Both queries when executed separately, execute without any error. How do I achieve this ?
EDIT:
Also tried this with IF,
SELECT if(count(*)==0,INSERT INTO games(cateID ,time,users,active)
VALUES ('2', '1', '0', '1')) FROM `games` WHERE cateID=2 AND active=1
Still gives the same error. EDIT 2: By the suggested comment,
INSERT INTO games(cateID ,time,users,active)
select '2','2','0','1'
where (SELECT count(*) FROM `games` WHERE cateID=2 AND active=1) <= 0
Still gives error.