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

StackOverflow https://stackoverflow.com/questions/23675608

  •  23-07-2023
  •  | 
  •  

Вопрос

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

Это было полезно?

Решение

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

Другие советы

try this:

update your_table 
set field = if(id = 1 ,'featured','non_featured')
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top