How To Swap Column Values Of 2 Rows In MySQL

Setting the sort order between 2 rows in MySQL can be done in 1 line by making use of the IF() function.

I had a scenario where I had move up and move down functionalities to set the order of my database entries.

Let us say, you have columns ID and SORT_ORDER.

This line of code should do the trick.

Related Posts Plugin for WordPress, Blogger...

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">