Check if value exists before doing update [closed]

-8

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

    
asked by anonymous 19.08.2015 / 13:36

1 answer

4

The ideal is to leave the field UNIQUE , but if for some reason you need to do, 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')
    
19.08.2015 / 13:48