Jump to content

A should-be-simple mysql query


tibberous

Recommended Posts

You have a table:

 

1 Bill

2 Bob

3 Scott

 

In one query, is there a way to switch `Bill` and `Bob`?

 

And no, this isn't my homework problem :) I just simplified the problem I'm having.

 

If it help, I found this code, which is supposed to switch the values between columns in a single row:

 

update `table` set `a`=`b`, `b`=@old where (@old:=`a`);

 

I know it would only be like three lines of PHP, but it seems pretty basic not to know...

Link to comment
https://forums.phpfreaks.com/topic/102537-a-should-be-simple-mysql-query/
Share on other sites

DELIMITER ;;

CREATE DEFINER=`root`@`localhost` PROCEDURE `swap`(p1 int, p2 int)

begin

set @v1 := (select `Name` from `test` where id=p1);

set @v2 := (select `Name` from `test` where id=p2);

update `test` set `Name`:=@v2 where id=p1;

update `test` set `Name`=@v1 where id=p2;

end;;

DELIMITER ;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.