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 4126220
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:58:20+00:00 2026-05-20T23:58:20+00:00

There is a way that MySQL to check record which is going to INSERT

  • 0

There is a way that MySQL to check record which is going to INSERT is duplicate or not is using:

INSERT ... ON DUPLICATE KEY UPDATE 

But as the document says: the KEY to check is the primary key.And in my situation,I need to check tow fields (columns) to make sure it is duplicate or not.My table structure:

auto_id  user_id  file_id file_status
1        1        12        1
2        3        12        0
3        1        17        1
4        4        31        1
5        1        41        0
6        4        31        0
7        1        18        1
8        5        11        0
9        1        10        0

E.g:Maybe like:

ON DUPLICATE user_id,file_id UPDATE file_status = 0;

if user_id and file_id which in one record were duplicate,I will update the file_status to 0.

Thank you very much!!

[update]

So,I have to do it in several steps!?

First, find out the records exist or not:

SELECT auto_id FROM MyTable WHERE user_id='user_id' AND file_id='file_id'.

Second, decide to INSERT new record or UPDATE according to the SELECT result!

  • 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-20T23:58:21+00:00Added an answer on May 20, 2026 at 11:58 pm

    You can also create a stored procedure and check if the file_id already exists or not:

    drop if procedure exists check_user_files;
    create procedure check_user_files
    (
        in p_file_id smallint
    )
    begin
        declare v_success tinyint unsigned default 0;
    
        if not exists (select 1 from user_files where file_id = p_file_id) then
    
            [....] -- insert statement
    
        else
    
            [....] -- remove or update
    
        end if;
    
        select v_success as success;
    end;
    

    And where are u using auto_id for? I think its unnecessary here?

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

Sidebar

Related Questions

Using standard mysql functions is there a way to write a query that will
Is there a way to encrypt the data file that mysql uses? I have
Is there a simple way to cache MySQL queries in PHP or failing that,
Possible Duplicate: Is there a way in MySQL to reverse a Boolean field with
Is there any way that I can programmatically create (and I guess access) hidden
Is there any way that I can change how a Literal of a code
Is there a way that you can have SERVEROUTPUT set to ON in sqlplus
Is there a way that my .NET CF app running on a Windows CE
Is there a way that I can run a ASP.NET MVC Project on godaddy.com
Is there any way that I could get the source of a website (as

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.