Arun
Arun

Reputation: 3731

update data in particular id and delete all other in single query.mysql

In my website, I have an image table, where I store images of different products. I want to mark an Image as featured, so which will show at the front end. I give a radio button to mark it as featured. My problem is, if i already have an image as featured, and i want to update my field, how can i do the same with single query by add the new image as featured, and delete the old "featured" data.

i need something like this

update table set field=if id="1" then update to featured else update to non-featured

please help

Upvotes: 0

Views: 66

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 1549

try this:

update your_table 
set field = if(id = 1 ,'featured','non_featured')

Upvotes: 0

juergen d
juergen d

Reputation: 204904

update your_table 
set field = case when id = 1 then 'featured' 
                             else 'non_featured'
            end

Upvotes: 1

Related Questions