mysql - SQL query with two different ids -


i need prevent photo_order field exceeding number of photos being associated article. have this, doesn't work. table name has been used 2 times , wrong.

update articles_photos set photo_order =      if(photo_order < (select count(id) articles_photos         article_id = 12), photo_order + 1, 1) id = 26 

how can fix above query? database mysql.

i assume error getting:

you can't specify target table 'articles_photos' update in clause

here's 1 work around using cross join subquery:

update articles_photos ap cross join (select count(id) cnt articles_photos article_id = 12) temp set ap.photo_order = if(ap.photo_order<temp.cnt,ap.photo_order+1,1) ap.id = 26; 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -