I am making a daily counter for a blog and I have problem with this query :
IF EXISTS SELECT * FROM tableA WHERE blog_id=1
UPDATE FROM tableA SET c=c+'1' WHERE blog_id='1' AND c_date=NOW()
ELSE
INSERT INTO FROM tableA VALUES (blog_id,c,c_date) VALUES (1,1,now())
its showing following error :
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 ‘IF EXISTS SELECT * FROM tableA WHERE blog_id=1 UPDATE FROM
tableA SET c=c+” at line 1
My table entries should look like this :
id , blod_id , c , c_date
1 , 1001, 66 , 2011-11-11
2 , 1001, 160 , 2011-11-12
3 , 1002, 200 , 2011-11-12
4 , 1003, 33 , 2011-11-12
I suspect you’re lokoing for the
insert into on duplicate key updatesyntaxFrom the manual:
http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
Your query should look more like the following example:
You need to have a
UNIQUEkey or aPRIMARY KEYfor this to work.Based on your table example you could add a primary key to the
idcolumn or add a unique composite index on the(c_date, blog_id)fieldsand use this: