Check if value exists before updating

Asked

Viewed 878 times

-8

I need to make a query to update a column, but you should only update the column if the data to be updated does not currently exist in that column.

  • 1

    If there is no?

  • simply does nothing, I just want to udpate if this does not exist in the table already, I am able to arrange with update if not exist Insert

  • From what I understand what you want is a INSERT if the record does not exist in the table.

  • no, I want to update a field. but I don’t want repeated values.

  • Specify. Give an example of what you want. If you don’t want repeated values in a column, then determine that it should be UNIQUE.

  • 3

    Hello Miguel! Welcome to Stack Overflow. Please make a tour to know how to get better answers. Your question is a little confusing, you need to drill in better. Put the SQL command you tried to do, an example of how you would like the information to be after the update, etc. We need more information to help you.

  • we have a table with this data Joao Luis and Joao Sousa, I want to update to Joao Luis I want to change also to Joao Sousa but will only make this update if the table does not exist if there is nothing.

  • @Let me get this straight, you want to update a column of a table only if the value to be updated does not currently exist in that column of the table. Would that be?

  • 1

    @Erloncharles yes that’s it! Thank you

Show 4 more comments

1 answer

4

The ideal is to leave the field UNIQUE, but if for some reason you need to do it, you can use a query similar to this:

UPDATE 
    suatabela 
SET 
    seucampo = 'seuvalor' 
WHERE 
    seucampo NOT IN (SELECT 
                        COUNT(seucampo) 
                    FROM 
                        suatabela 
                    WHERE 
                        seucampo = 'seuvalor')
  • Check if value exists before you update, this is what I want to do

  • this is not what I want

Browser other questions tagged

You are not signed in. Login or sign up in order to post.