[DB][mariadb] Query to exchange values of specific columns in two rows
Situation
a query to exchange specific column values for two selected rows in a table
- swap the order of two files in a specific folder
mysql, mariadb debi only queries
Sample table schema
SQLCREATE TABLE `tm_db` (
`seq` int(11) NOT NULL,
`orders` int(11) DEFAULT NULL,
PRIMARY KEY (`seq`)
);
- seq - primary key
- orders - column value you want to exchange
when the data exists like below
TXTseq|orders|
---+------+
1| 12| <<
2| 8|
3| 7| <<
want to swap orders 1 and 3
update join query
update queryUPDATE tm_db AS t1
JOIN tm_db AS t2
ON t1.seq =1 AND t2.seq = 3
SET
t1.orders = t2.orders,
t2.orders = t1.orders;
Resulting table
TXTseq|orders|
---+------+
1| 7| <<
2| 8|
3| 12| <<