<?xml version="1.0" encoding="UTF-8"?><rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
> <channel><title>Comments on: Swapping Column Values in MySQL</title> <atom:link href="http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/feed/" rel="self" type="application/rss+xml" /><link>http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/</link> <description>where things have nothing to do with beer - tutorials, tips, how-tos, thoughts, hacks, and other techy nonsense</description> <lastBuildDate>Sat, 11 Feb 2012 17:00:25 +0000</lastBuildDate> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <item><title>By: ali</title><link>http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-50956</link> <dc:creator>ali</dc:creator> <pubDate>Wed, 22 Sep 2010 21:24:42 +0000</pubDate> <guid
isPermaLink="false">http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-50956</guid> <description>I don&#039;t know if any person post this approach already:
UPDATE tblone t1, tblone t2
SET
t1.Column=(@temp:=t1.Column),
t1.Column = t2.Column ,
t2.Column = @temp
WHERE
t1.ID=10 and t2.ID=20</description> <content:encoded><![CDATA[<p>I don&#039;t know if any person post this approach already:</p><p>UPDATE tblone t1, tblone t2<br
/> SET<br
/> t1.Column=(@temp:=t1.Column),<br
/> t1.Column = t2.Column ,<br
/> t2.Column = @temp<br
/> WHERE<br
/> t1.ID=10 and t2.ID=20</p> ]]></content:encoded> </item> <item><title>By: Alex</title><link>http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-30570</link> <dc:creator>Alex</dc:creator> <pubDate>Wed, 13 Jan 2010 14:10:44 +0000</pubDate> <guid
isPermaLink="false">http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-30570</guid> <description>What about swapping the id column of table 1, with another column from table 2?
Can I apply any of these methods to swap columns across tables?</description> <content:encoded><![CDATA[<p>What about swapping the id column of table 1, with another column from table 2?</p><p>Can I apply any of these methods to swap columns across tables?</p> ]]></content:encoded> </item> <item><title>By: Maresa</title><link>http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-25203</link> <dc:creator>Maresa</dc:creator> <pubDate>Tue, 13 Oct 2009 17:36:40 +0000</pubDate> <guid
isPermaLink="false">http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-25203</guid> <description>@Dogo: Swapping values between 2 rows from the same table is explained on my post here: http://www.microshell.com/database/sql/swap-values-in-2-rows-sql/
In a sense, it&#039;s basically the same solution as this post. You do self join on the table, then swap columns.</description> <content:encoded><![CDATA[<p>@Dogo: Swapping values between 2 rows from the same table is explained on my post here: <a
href="http://www.microshell.com/database/sql/swap-values-in-2-rows-sql/" rel="nofollow">http://www.microshell.com/database/sql/swap-values-in-2-rows-sql/</a></p><p>In a sense, it&#039;s basically the same solution as this post. You do self join on the table, then swap columns.</p> ]]></content:encoded> </item> <item><title>By: Artem Russakovskii</title><link>http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-21420</link> <dc:creator>Artem Russakovskii</dc:creator> <pubDate>Sat, 01 Aug 2009 03:40:58 +0000</pubDate> <guid
isPermaLink="false">http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-21420</guid> <description>@Dogo
Try posting this at http://www.stackoverflow.com. Should have a discussion going in no titme.</description> <content:encoded><![CDATA[<p>@Dogo<br
/> Try posting this at <a
href="http://www.stackoverflow.com" rel="nofollow">http://www.stackoverflow.com</a>. Should have a discussion going in no titme.</p> ]]></content:encoded> </item> <item><title>By: Dogo</title><link>http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-21417</link> <dc:creator>Dogo</dc:creator> <pubDate>Sat, 01 Aug 2009 01:25:29 +0000</pubDate> <guid
isPermaLink="false">http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-21417</guid> <description>Hi,
i looked around a lot but do not seem to get it right:
I&#039;d like to swap two values in different lines. As entry id 14 (e.g) has &quot;order&quot; 5 and another has &quot;order&quot; 6.
Is there any way to swap these two within one single query? I already have the id (14) and order of the first one.
I ran through different approaches like &quot;ORDER BY order DESC LIMIT 1&quot; but it always takes two queries...
Best Regards,
Dogo</description> <content:encoded><![CDATA[<p>Hi,</p><p>i looked around a lot but do not seem to get it right:<br
/> I&#039;d like to swap two values in different lines. As entry id 14 (e.g) has &#034;order&#034; 5 and another has &#034;order&#034; 6.<br
/> Is there any way to swap these two within one single query? I already have the id (14) and order of the first one.</p><p>I ran through different approaches like &#034;ORDER BY order DESC LIMIT 1&#034; but it always takes two queries&#8230;</p><p>Best Regards,</p><p>Dogo</p> ]]></content:encoded> </item> <item><title>By: Handy</title><link>http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-17903</link> <dc:creator>Handy</dc:creator> <pubDate>Thu, 14 May 2009 21:42:52 +0000</pubDate> <guid
isPermaLink="false">http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-17903</guid> <description>I&#039;ve used
(&#039;1&#039;, &#039;a&#039;, &#039;10&#039;)
, (&#039;2&#039;, null, &#039;20&#039;)
, (&#039;3&#039;, &#039;c&#039;, &#039;30&#039;)
It&#039;s great ! :)</description> <content:encoded><![CDATA[<p>I&#039;ve used<br
/> (&#039;1&#039;, &#039;a&#039;, &#039;10&#039;)<br
/> , (&#039;2&#039;, null, &#039;20&#039;)<br
/> , (&#039;3&#039;, &#039;c&#039;, &#039;30&#039;)<br
/> It&#039;s great ! <img
src='http://beerpla.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p> ]]></content:encoded> </item> <item><title>By: Mathijs</title><link>http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-16774</link> <dc:creator>Mathijs</dc:creator> <pubDate>Wed, 29 Apr 2009 13:42:30 +0000</pubDate> <guid
isPermaLink="false">http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-16774</guid> <description>maybe you can do something with this aswell :P
had some problems with switching integer values in 2 different rows with eachother.
they are used to describe an order, and eacht time there will be a difference of one.
this is what did the trick.
UPDATE tablename AS first, tablename AS second, tablename AS third
SET first.ord = second.ord, second.ord = third.ord
WHERE first.parent = second.parent AND first.id = 1 AND first.id = third.id AND second.ord = third.ord-1 AND first.status &gt; 0 AND second.status &gt; 0</description> <content:encoded><![CDATA[<p>maybe you can do something with this aswell <img
src='http://beerpla.net/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /><br
/> had some problems with switching integer values in 2 different rows with eachother.<br
/> they are used to describe an order, and eacht time there will be a difference of one.<br
/> this is what did the trick.</p><p>UPDATE tablename AS first, tablename AS second, tablename AS third<br
/> SET first.ord = second.ord, second.ord = third.ord<br
/> WHERE first.parent = second.parent AND first.id = 1 AND first.id = third.id AND second.ord = third.ord-1 AND first.status &gt; 0 AND second.status &gt; 0</p> ]]></content:encoded> </item> <item><title>By: MySQL Indexing Considerations Of Implementing A Priority Field In Your Application &#124; WORDPRESS EXTENSIONS-PLUGINS-THEMES-TEMPLATES</title><link>http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-14592</link> <dc:creator>MySQL Indexing Considerations Of Implementing A Priority Field In Your Application &#124; WORDPRESS EXTENSIONS-PLUGINS-THEMES-TEMPLATES</dc:creator> <pubDate>Thu, 19 Mar 2009 02:00:35 +0000</pubDate> <guid
isPermaLink="false">http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-14592</guid> <description>[...] Swapping Column Values in MySQL [...]</description> <content:encoded><![CDATA[<p>[...] Swapping Column Values in MySQL [...]</p> ]]></content:encoded> </item> <item><title>By: custom software developer</title><link>http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-13920</link> <dc:creator>custom software developer</dc:creator> <pubDate>Mon, 02 Mar 2009 11:49:29 +0000</pubDate> <guid
isPermaLink="false">http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-13920</guid> <description>Thanks for that helpful post. that was awesome ;)I shall necessarily try</description> <content:encoded><![CDATA[<p>Thanks for that helpful post. that was awesome <img
src='http://beerpla.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> I shall necessarily try</p> ]]></content:encoded> </item> <item><title>By: Peter Gulutzan</title><link>http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-13603</link> <dc:creator>Peter Gulutzan</dc:creator> <pubDate>Sun, 22 Feb 2009 00:58:24 +0000</pubDate> <guid
isPermaLink="false">http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-13603</guid> <description>We&#039;re aware of the problem.
http://forge.mysql.com/worklog/task.php?id=927</description> <content:encoded><![CDATA[<p>We&#039;re aware of the problem.<br
/> <a
href="http://forge.mysql.com/worklog/task.php?id=927" rel="nofollow">http://forge.mysql.com/worklog/task.php?id=927</a></p> ]]></content:encoded> </item> <item><title>By: Log Buffer #136: A Carnival of the Vanities for DBAs</title><link>http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-13478</link> <dc:creator>Log Buffer #136: A Carnival of the Vanities for DBAs</dc:creator> <pubDate>Fri, 20 Feb 2009 17:01:43 +0000</pubDate> <guid
isPermaLink="false">http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-13478</guid> <description>[...] On beer planet, Artem Russakovskii publishes his findings on swapping column values in MySQL. [...]</description> <content:encoded><![CDATA[<p>[...] On beer planet, Artem Russakovskii publishes his findings on swapping column values in MySQL. [...]</p> ]]></content:encoded> </item> <item><title>By: Intercambiando el valor de columnas en MYSQL &#124; dominios, diseño web, ecommerce - Mantis Technology Solutions Blog</title><link>http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-13373</link> <dc:creator>Intercambiando el valor de columnas en MYSQL &#124; dominios, diseño web, ecommerce - Mantis Technology Solutions Blog</dc:creator> <pubDate>Thu, 19 Feb 2009 05:02:47 +0000</pubDate> <guid
isPermaLink="false">http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-13373</guid> <description>[...] Swapping Column Values in MySQL [...]</description> <content:encoded><![CDATA[<p>[...] Swapping Column Values in MySQL [...]</p> ]]></content:encoded> </item> <item><title>By: Artem Russakovskii</title><link>http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-13344</link> <dc:creator>Artem Russakovskii</dc:creator> <pubDate>Wed, 18 Feb 2009 20:06:14 +0000</pubDate> <guid
isPermaLink="false">http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-13344</guid> <description>@Dipin
Brilliant. That indeed works for all test cases. I think this is finally the cleanest and most elegant solution.</description> <content:encoded><![CDATA[<p>@Dipin<br
/> Brilliant. That indeed works for all test cases. I think this is finally the cleanest and most elegant solution.</p> ]]></content:encoded> </item> <item><title>By: Lenguajes X &#187; Intercambiando el valor de columnas en MYSQL</title><link>http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-13338</link> <dc:creator>Lenguajes X &#187; Intercambiando el valor de columnas en MYSQL</dc:creator> <pubDate>Wed, 18 Feb 2009 19:28:40 +0000</pubDate> <guid
isPermaLink="false">http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-13338</guid> <description>[...] Swapping Column Values in MySQL [...]</description> <content:encoded><![CDATA[<p>[...] Swapping Column Values in MySQL [...]</p> ]]></content:encoded> </item> <item><title>By: Dipin Hora</title><link>http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-13332</link> <dc:creator>Dipin Hora</dc:creator> <pubDate>Wed, 18 Feb 2009 18:10:14 +0000</pubDate> <guid
isPermaLink="false">http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-13332</guid> <description>update swap_test_1 set x=(@temp:=x), x = y, y = @temp;
Works for all scenarios based on my quick tests.</description> <content:encoded><![CDATA[<p>update swap_test_1 set x=(@temp:=x), x = y, y = @temp;</p><p>Works for all scenarios based on my quick tests.</p> ]]></content:encoded> </item> <item><title>By: Artem Russakovskii</title><link>http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-13325</link> <dc:creator>Artem Russakovskii</dc:creator> <pubDate>Wed, 18 Feb 2009 15:41:36 +0000</pubDate> <guid
isPermaLink="false">http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-13325</guid> <description>@Dmitri
You&#039;re absolutely right. I had it in the back of my mind to test the NULL case but it escaped me till now. So, approach #2 doesn&#039;t swap the values if one of them is NULL. Approach #3 still works fine. I&#039;ll make a note of it, until a new approach is developed for #2.</description> <content:encoded><![CDATA[<p>@Dmitri<br
/> You&#039;re absolutely right. I had it in the back of my mind to test the NULL case but it escaped me till now. So, approach #2 doesn&#039;t swap the values if one of them is NULL. Approach #3 still works fine. I&#039;ll make a note of it, until a new approach is developed for #2.</p> ]]></content:encoded> </item> <item><title>By: Dmitri Mikhailov</title><link>http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-13324</link> <dc:creator>Dmitri Mikhailov</dc:creator> <pubDate>Wed, 18 Feb 2009 15:32:19 +0000</pubDate> <guid
isPermaLink="false">http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-13324</guid> <description>*** use this record set:
insert into swap_test_1 values
(&#039;1&#039;, &#039;a&#039;, &#039;10&#039;)
, (&#039;2&#039;, null, &#039;20&#039;)
, (&#039;3&#039;, &#039;c&#039;, &#039;30&#039;)
;</description> <content:encoded><![CDATA[<p>*** use this record set:</p><p>insert into swap_test_1 values<br
/> (&#039;1&#039;, &#039;a&#039;, &#039;10&#039;)<br
/> , (&#039;2&#039;, null, &#039;20&#039;)<br
/> , (&#039;3&#039;, &#039;c&#039;, &#039;30&#039;)<br
/> ;</p> ]]></content:encoded> </item> <item><title>By: Dmitri Mikhailov</title><link>http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-13322</link> <dc:creator>Dmitri Mikhailov</dc:creator> <pubDate>Wed, 18 Feb 2009 15:29:10 +0000</pubDate> <guid
isPermaLink="false">http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-13322</guid> <description>I would not recommend to use this approach, as the update fails to swap values if one of them is null:
drop table if exists swap_test_1
;
create table swap_test_1
(
id int          not null auto_increment
, x  varchar(255)     null
, y  varchar(255)     null
, primary key     (id)
) engine=innodb;
insert into swap_test_1 values
(&#039;1&#039;, &#039;a&#039;, &#039;10&#039;)
, (&#039;2&#039;, &#039;b&#039;, null)
, (&#039;3&#039;, &#039;c&#039;, &#039;30&#039;)
;
update swap_test_1 set x=y, y=@temp where (@temp:=x) is not null;
select * from swap_test_1;</description> <content:encoded><![CDATA[<p>I would not recommend to use this approach, as the update fails to swap values if one of them is null:</p><p>drop table if exists swap_test_1<br
/> ;<br
/> create table swap_test_1<br
/> (<br
/> id int          not null auto_increment<br
/> , x  varchar(255)     null<br
/> , y  varchar(255)     null<br
/> , primary key     (id)<br
/> ) engine=innodb;</p><p>insert into swap_test_1 values<br
/> (&#039;1&#039;, &#039;a&#039;, &#039;10&#039;)<br
/> , (&#039;2&#039;, &#039;b&#039;, null)<br
/> , (&#039;3&#039;, &#039;c&#039;, &#039;30&#039;)<br
/> ;</p><p>update swap_test_1 set x=y, y=@temp where (@temp:=x) is not null;</p><p>select * from swap_test_1;</p> ]]></content:encoded> </item> <item><title>By: Pabloj</title><link>http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-13314</link> <dc:creator>Pabloj</dc:creator> <pubDate>Wed, 18 Feb 2009 13:02:47 +0000</pubDate> <guid
isPermaLink="false">http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-13314</guid> <description>All major databases &quot;think is sets&quot; and make the swap seamless, if I remember well just MySQL and Firebird don&#039;t work this way.</description> <content:encoded><![CDATA[<p>All major databases &#034;think is sets&#034; and make the swap seamless, if I remember well just MySQL and Firebird don&#039;t work this way.</p> ]]></content:encoded> </item> <item><title>By: Artem Russakovskii</title><link>http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-13299</link> <dc:creator>Artem Russakovskii</dc:creator> <pubDate>Wed, 18 Feb 2009 10:04:00 +0000</pubDate> <guid
isPermaLink="false">http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-13299</guid> <description>@Roland
Yeah, that makes sense, and I thought the caveat  was worth pointing out, seeing how some examples on the web are omitting parentheses.
As far as the first query, I just tested both MyISAM and InnoDB and they both behave the same - x and y end up equal, as expected. The &lt;a href=&quot;http://dev.mysql.com/doc/refman/6.0/en/update.html&quot; rel=&quot;nofollow&quot;&gt;MySQL manual&lt;/a&gt; confirms it: &quot;Single-table UPDATE assignments are generally evaluated from left to right.&quot;</description> <content:encoded><![CDATA[<p>@Roland<br
/> Yeah, that makes sense, and I thought the caveat  was worth pointing out, seeing how some examples on the web are omitting parentheses.</p><p>As far as the first query, I just tested both MyISAM and InnoDB and they both behave the same &#8211; x and y end up equal, as expected. The <a
href="http://dev.mysql.com/doc/refman/6.0/en/update.html" rel="nofollow">MySQL manual</a> confirms it: &#034;Single-table UPDATE assignments are generally evaluated from left to right.&#034;</p> ]]></content:encoded> </item> </channel> </rss>
