Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6809509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:05:42+00:00 2026-05-26T20:05:42+00:00

I am making a daily counter for a blog and I have problem with

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:05:42+00:00Added an answer on May 26, 2026 at 8:05 pm

    I suspect you’re lokoing for the insert into on duplicate key update syntax

    From the manual:

    12.2.5.3. INSERT … ON DUPLICATE KEY UPDATE Syntax

    If you specify ON DUPLICATE KEY UPDATE, and a row is inserted that would cause a
    duplicate value in a UNIQUE index or PRIMARY KEY, an UPDATE of the old
    row is performed.

    http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

    Your query should look more like the following example:

    INSERT INTO tableA (blog_id,c,c_date) VALUES (1,1,now()) ON DUPLICATE KEY UPDATE `c` = `c`+1, `c_date` = NOW()
    

    You need to have a UNIQUE key or a PRIMARY KEY for this to work.

    ALTER TABLE  `table` ADD PRIMARY KEY (  `id` )
    

    Based on your table example you could add a primary key to the id column or add a unique composite index on the (c_date, blog_id) fields

    ALTER TABLE  `table` ADD UNIQUE (
    blog_id ,
    c_date
    );
    

    and use this:

    INSERT INTO tableA (blog_id, c, c_date) 
      VALUES (1, 1, NOW()) 
    ON DUPLICATE KEY UPDATE 
      c = c + 1                --- update only `c`, not any part of the unique key
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Making a word document of our network set-up. We have about 7 servers and
Making my first steps in RIA Services (VS2010Beta2) and i encountered this problem: created
I am making an app where the user gets daily book recommendations from an
Within my daily work, I have got to maximize a particular function making use
im making an application where i have to load my database from html file
Making websites that appear correctly in IE is a big problem. Is there any
I have a MySQL high scores table for a game that shows the daily
Making a mobile friendly site, I have a single field and a submit button.
GA Unique Visitors data isn't making sense to me. From the GA FAQ we
Many tutorial sites mention making hourly/daily/weekly backups with rsync [1] , [2] , and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.