I want to swap the values from two rows in a table. I have the rows IDs of the two rows. Is there any query to do that? Here is an example. Before the query I have this:
I had a similar issue recently I had a column for ordering the output and wanted to allow moving the order around. I was looking for the answer and ran across this question. This didn't sufficiently answer my particular query but maybe my solution will help others.
I had my database look like so
Table:Order_Table
Index_Column,Order_Column,Text
1 ,1 ,"Second Test text"
2 ,2 ,"First Test text"
I wanted to be able to swap them around using pdo in php. Ultimately I found a way to do it with one SQL query
UPDATE `Order_Table` AS o
INNER JOIN (SELECT `Index_Column`, `Order_Column` FROM `Order_Table`
WHERE `Index_Column` IN (:Index1,:Index2))
AS t ON o.`Index_Column` <> t.`Index_Column`
SET o.`Order_Column` = t.`Order_Column`
WHERE o.`Index_Column` IN (:Index1,:Index2)