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

  • SEARCH
  • Home
  • 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 6868067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:23:18+00:00 2026-05-27T03:23:18+00:00

I have the following query (user_id isn’t and can’t be primary key as there

  • 0

I have the following query (user_id isn’t and can’t be primary key as there are many entries from each user depending on another column of the table). I used duplicate key but doesnt work. It still adds a new row. Any thoughts?

INSERT INTO profile (user_id, correct) VALUES(". $user_id . ", correct + 1)
ON DUPLICATE KEY UPDATE correct=correct+1

EDIT
So if row exists for this user_id, I want to go update this entry and SET correct = correct+1 and not make a new entry with user_id and correct=1 again

  • 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-27T03:23:18+00:00Added an answer on May 27, 2026 at 3:23 am

    Try this:

    INSERT IGNORE INTO profile (user_id, correct)
    VALUES ($user_id, 0);
    
    UPDATE profile SET correct = correct + 1 
    WHERE  user_id = $user_id;  -- updates 0 to 1
    

    Or:

    UPDATE profile SET correct = correct + 1 
    WHERE  user_id = $user_id;
    

    If not found:

    INSERT INTO profile (user_id, correct)
    VALUES ($user_id, 1);
    

    I prefer the second variant.
    The first variant requires that the user_id has a UNIQUE constraint. (PRIMARY KEY counts.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following query: $query=SELECT language_value, votes, user_id FROM labels WHERE approved=1 AND label_value=.
I have the following query in Doctrine2. $dql->select('um', 'u', 'r') ->from('AcmeComBundle:UserMenu', 'um') ->join('um.user', 'u')
I have the following query SELECT s.s_id, s.t_id, c.c_id, c.desc, sm.user_id FROM s s
I have the following query SELECT * FROM (`user_profiles`) WHERE `user_id` = $user_id LIMIT
I have a following oracle query: SELECT a.USER_ID, c.first_name, c.last_name, TO_CHAR( b.logon_date, 'MM/DD/YYYY HH:MI:SS
I have the following query: SELECT * FROM quotes INNER JOIN quotes_panels ON quotes.wp_user_id
I have this following query for CodeIgniter: $q = $this->db->where('(message_from='.$user_id.' AND message_to='.$this->auth_model->userdata['user_id'].')') ->or_where('(message_from='.$this->auth_model->userdata['user_id'].' AND
I have an application that executes the following MySQL query: SELECT 402 AS user_id,
I have following query , select * from process where name like 'abc'; now
I have following query which take more than a minute to execute, how can

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.