switching the value of a field between two rows - MySQL

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
SimonMayer
Forum Commoner
Posts: 32
Joined: Wed Sep 09, 2009 6:40 pm

switching the value of a field between two rows - MySQL

Post by SimonMayer »

I have multiple rows in a MySQL database and I wish to be swap the value of one field between two rows
I am not looking to switch the primary key ID, but a separate ID stored in another field, which I am using to determine the displayed order on a webpage. I can think of ways to do it through PHP, but they are very clunky. Is there a MySQL query that will handle this?

I currently have:

Article_ID | Title | Order_ID
1 | Article One | 1
2 | Article Two | 2
3 | Article Three | 3


I wish to switch the Order_IDs of rows 2 and 3, so that I end up with:

Article_ID | Title | Order_ID
1 | Article One | 1
2 | Article Two | 3
3 | Article Three | 2
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: switching the value of a field between two rows - MySQL

Post by VladSun »

What SQL queries have you tried so far?
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply