<?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, 19 May 2012 21:39:38 +0000</lastBuildDate> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.2</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: 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: 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: 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> <item><title>By: Roland Bouman</title><link>http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-13295</link> <dc:creator>Roland Bouman</dc:creator> <pubDate>Wed, 18 Feb 2009 09:50:16 +0000</pubDate> <guid
isPermaLink="false">http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-13295</guid> <description>Hi!
&quot;In the first query, the missing parentheses will cause data corruption. I will make sure to point it out and correct my example.&quot;
I think it&#039;s a matter of operator precedence. Look here: http://dev.mysql.com/doc/refman/5.1/en/operator-precedence.html
As you can see, the := binds weakest so omitting the parenthesis will cause
x IS NOT NULL
to be evaluated first. That expression evaluates to a boolean value, but MySQL doesn&#039;t have those so it becomes either 1 (TRUE) or 0 (FALSE)
As for the obvious first query,
UPDATE swap_test SET x=y, y=x;
do you notice any difference between InnoDB and MyISAM? Personally I think this query should just work but I am aware of the fact that MySQL takes a few shortcuts in the UPDATE implementation.</description> <content:encoded><![CDATA[<p>Hi!</p><p>&#034;In the first query, the missing parentheses will cause data corruption. I will make sure to point it out and correct my example.&#034;</p><p>I think it&#039;s a matter of operator precedence. Look here: <a
href="http://dev.mysql.com/doc/refman/5.1/en/operator-precedence.html" rel="nofollow">http://dev.mysql.com/doc/refman/5.1/en/operator-precedence.html</a></p><p>As you can see, the := binds weakest so omitting the parenthesis will cause</p><p>x IS NOT NULL</p><p>to be evaluated first. That expression evaluates to a boolean value, but MySQL doesn&#039;t have those so it becomes either 1 (TRUE) or 0 (FALSE)</p><p>As for the obvious first query,</p><p>UPDATE swap_test SET x=y, y=x;</p><p>do you notice any difference between InnoDB and MyISAM? Personally I think this query should just work but I am aware of the fact that MySQL takes a few shortcuts in the UPDATE implementation.</p> ]]></content:encoded> </item> <item><title>By: Artem Russakovskii</title><link>http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-13278</link> <dc:creator>Artem Russakovskii</dc:creator> <pubDate>Wed, 18 Feb 2009 07:51:31 +0000</pubDate> <guid
isPermaLink="false">http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-13278</guid> <description>@Antony
Thanks for all the hints.
I definitely didn&#039;t want to go the table rename approach, as MySQL is indeed quite dumb at making the slightest changes to the schema (even the .frm hack could be adopted by the server natively, why in the world hasn&#039;t that happened yet?).
In my particular case, I only wanted to modify a very small subset of the data.</description> <content:encoded><![CDATA[<p>@Antony<br
/> Thanks for all the hints.<br
/> I definitely didn&#039;t want to go the table rename approach, as MySQL is indeed quite dumb at making the slightest changes to the schema (even the .frm hack could be adopted by the server natively, why in the world hasn&#039;t that happened yet?).</p><p>In my particular case, I only wanted to modify a very small subset of the data.</p> ]]></content:encoded> </item> <item><title>By: Artem Russakovskii</title><link>http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-13275</link> <dc:creator>Artem Russakovskii</dc:creator> <pubDate>Wed, 18 Feb 2009 04:52:12 +0000</pubDate> <guid
isPermaLink="false">http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-13275</guid> <description>@Arjen
What kind of situation and what is the flaw in the schema? Can you provide more details please?</description> <content:encoded><![CDATA[<p>@Arjen<br
/> What kind of situation and what is the flaw in the schema? Can you provide more details please?</p> ]]></content:encoded> </item> <item><title>By: Artem Russakovskii</title><link>http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-13274</link> <dc:creator>Artem Russakovskii</dc:creator> <pubDate>Wed, 18 Feb 2009 04:49:03 +0000</pubDate> <guid
isPermaLink="false">http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comment-13274</guid> <description>I just noticed one horrible, subtle thing:
&lt;pre line=&quot;0&quot;&gt;UPDATE swap_test SET x=y, y=@temp WHERE @temp:=x IS NOT NULL;&lt;/pre&gt;
vs
&lt;pre line=&quot;0&quot;&gt;UPDATE swap_test SET x=y, y=@temp WHERE (@temp:=x) IS NOT NULL;&lt;/pre&gt;
In the first query, the missing parentheses will cause data corruption. I will make sure to point it out and correct my example.</description> <content:encoded><![CDATA[<p>I just noticed one horrible, subtle thing:</p><div
class="wp_syntax"><div
class="code"><pre>UPDATE swap_test SET x=y, y=@temp WHERE @temp:=x IS NOT NULL;</pre></div></div><p>vs</p><div
class="wp_syntax"><div
class="code"><pre>UPDATE swap_test SET x=y, y=@temp WHERE (@temp:=x) IS NOT NULL;</pre></div></div><p>In the first query, the missing parentheses will cause data corruption. I will make sure to point it out and correct my example.</p> ]]></content:encoded> </item> </channel> </rss>
