Nested Updated MySQL Query -


table - galleries

+----+----------+------------+----------+ | id | user_id  | is_primary |  photo   | +----+----------+------------+----------+ |  1 | 1        | 1          | img1.jpg | |  2 | 2        | 1          | img2.jpg | |  3 | 1        | 0          | img3.jpg | |  4 | 1        | 0          | img4.jpg | |  5 | 1        | 0          | img5.jpg | |  6 | 3        | 1          | img6.jpg | |  7 | 2        | 0          | img7.jpg | +----+----------+------------+----------+  update galleries set is_primary=0 user_id=1 update galleries set is_primary=1 id=4 

there have column name is_primary need set 1 rows is_primary=1 user id 1 there have 1 row is_primary = 1 , user_id=1

i want updated is_primary=1 id=4 before updated need set is_primary=0 user_id=1.

i don't want write 2 times query updated.

how write nested query update record?

you can use case-when as

update galleries   set is_primary =   case     when id=4 1 else 0   end   user_id = 1 ; 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -