<?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: Swap values in 2 rows SQL</title>
	<atom:link href="http://www.microshell.com/database/sql/swap-values-in-2-rows-sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.microshell.com/database/sql/swap-values-in-2-rows-sql/</link>
	<description>Learn something share something</description>
	<lastBuildDate>Mon, 23 Jan 2012 10:40:22 -0800</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.1</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Maresa</title>
		<link>http://www.microshell.com/database/sql/swap-values-in-2-rows-sql/comment-page-1/#comment-17255</link>
		<dc:creator>Maresa</dc:creator>
		<pubDate>Fri, 06 Jan 2012 03:24:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.microshell.com/?p=1990#comment-17255</guid>
		<description>@Jimbo: I don&#039;t really get what you mean. You just need some kind of ID of the 2 rows to be swaped and swap the priority. If ID 1 with priority 19938 to be swapped with ID 48 with priority 38984, it really doesn&#039;t matter if there&#039;s priority 19937 or 19939 or 38983 or 38985.

The end result is ID 1 previously has priority 19938 should now have priority 38984. And ID 48 previously has priority 38984 should now have priority 19938.</description>
		<content:encoded><![CDATA[<p>@Jimbo: I don&#8217;t really get what you mean. You just need some kind of ID of the 2 rows to be swaped and swap the priority. If ID 1 with priority 19938 to be swapped with ID 48 with priority 38984, it really doesn&#8217;t matter if there&#8217;s priority 19937 or 19939 or 38983 or 38985.</p>
<p>The end result is ID 1 previously has priority 19938 should now have priority 38984. And ID 48 previously has priority 38984 should now have priority 19938.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jimbo</title>
		<link>http://www.microshell.com/database/sql/swap-values-in-2-rows-sql/comment-page-1/#comment-16373</link>
		<dc:creator>Jimbo</dc:creator>
		<pubDate>Thu, 15 Dec 2011 14:56:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.microshell.com/?p=1990#comment-16373</guid>
		<description>Problem with this code (while being very good) is that it relies on you neighbouring row having a id number -1 or + 1 OR knowing the id number of the row you&#039;re swapping with.

What happens when that neighbouring row has been deleted? - What do you do when there is several numbers between?

Is there no order by  asc / desc trick that can be used here?</description>
		<content:encoded><![CDATA[<p>Problem with this code (while being very good) is that it relies on you neighbouring row having a id number -1 or + 1 OR knowing the id number of the row you&#8217;re swapping with.</p>
<p>What happens when that neighbouring row has been deleted? &#8211; What do you do when there is several numbers between?</p>
<p>Is there no order by  asc / desc trick that can be used here?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: wydi</title>
		<link>http://www.microshell.com/database/sql/swap-values-in-2-rows-sql/comment-page-1/#comment-9890</link>
		<dc:creator>wydi</dc:creator>
		<pubDate>Fri, 06 May 2011 23:50:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.microshell.com/?p=1990#comment-9890</guid>
		<description>Thanks for this!

Just in case you\&#039;re curious:
For a categorized faq system I created the following table along with a couple of other columns:

 id &#124; parent &#124; sortorder &#124; content 
----+--------+-----------+------------------------
  1 &#124;   0    &#124;     1     &#124;  Category 1
----+--------+-----------+------------------------
  2 &#124;   1    &#124;     1     &#124;  Category 1, subitem 1
----+--------+-----------+------------------------
  3 &#124;   1    &#124;     2     &#124;  Category 1, subitem 2
----+--------+-----------+------------------------
  4 &#124;   1    &#124;     3     &#124;  Category 1, subitem 3
----+--------+-----------+------------------------
  5 &#124;   0    &#124;     2     &#124;  Category 2
----+--------+-----------+------------------------
  6 &#124;   5    &#124;     1     &#124;  Category 2, subitem 1
----+--------+-----------+------------------------

Guess, you get the idea.
Now, in the backend, I wanted to implement an option to move items up and down. Modifying the query from here, I came up with this:

UPDATE $table AS hc1 JOIN $table AS hc2 ON ( hc1.id = $id AND hc2.sortorder = hc1.sortorder+1 AND hc2.parent = hc1.parent ) SET hc1.sortorder = hc2.sortorder, hc2.sortorder = hc1.sortorder;

$table is (obviously) the table name, while $id is the ID of the item you want to move. The above query moves the item with $id down. For the other way around, change the + to a -.
I just wanted to share that. :)</description>
		<content:encoded><![CDATA[<p>Thanks for this!</p>
<p>Just in case you\&#8217;re curious:<br />
For a categorized faq system I created the following table along with a couple of other columns:</p>
<p> id | parent | sortorder | content<br />
&#8212;-+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
  1 |   0    |     1     |  Category 1<br />
&#8212;-+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
  2 |   1    |     1     |  Category 1, subitem 1<br />
&#8212;-+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
  3 |   1    |     2     |  Category 1, subitem 2<br />
&#8212;-+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
  4 |   1    |     3     |  Category 1, subitem 3<br />
&#8212;-+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
  5 |   0    |     2     |  Category 2<br />
&#8212;-+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
  6 |   5    |     1     |  Category 2, subitem 1<br />
&#8212;-+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>Guess, you get the idea.<br />
Now, in the backend, I wanted to implement an option to move items up and down. Modifying the query from here, I came up with this:</p>
<p>UPDATE $table AS hc1 JOIN $table AS hc2 ON ( hc1.id = $id AND hc2.sortorder = hc1.sortorder+1 AND hc2.parent = hc1.parent ) SET hc1.sortorder = hc2.sortorder, hc2.sortorder = hc1.sortorder;</p>
<p>$table is (obviously) the table name, while $id is the ID of the item you want to move. The above query moves the item with $id down. For the other way around, change the + to a -.<br />
I just wanted to share that. <img src='http://www.microshell.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Manoj</title>
		<link>http://www.microshell.com/database/sql/swap-values-in-2-rows-sql/comment-page-1/#comment-4939</link>
		<dc:creator>Manoj</dc:creator>
		<pubDate>Thu, 30 Sep 2010 05:17:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.microshell.com/?p=1990#comment-4939</guid>
		<description>Hi Guys, I m beginner in SQL.

Can any tell me how can i swap &quot;Take one bread to Wash hand&quot; and &quot;Wash hand to Take one bread&quot; in Above table.</description>
		<content:encoded><![CDATA[<p>Hi Guys, I m beginner in SQL.</p>
<p>Can any tell me how can i swap &#8220;Take one bread to Wash hand&#8221; and &#8220;Wash hand to Take one bread&#8221; in Above table.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vladimir Bilyov</title>
		<link>http://www.microshell.com/database/sql/swap-values-in-2-rows-sql/comment-page-1/#comment-4467</link>
		<dc:creator>Vladimir Bilyov</dc:creator>
		<pubDate>Thu, 19 Aug 2010 20:43:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.microshell.com/?p=1990#comment-4467</guid>
		<description>why not to simplify final query with:

01.UPDATE
02.    rules
03.SET
04.    priority = rule1.priority
05.FROM
06.    rules AS rule1
07.WHERE 
08.        rules.rule_id IN (1,4)
09.    AND rule1.rule_id IN (1,4)
10.    AND rules.rule_id &lt;&gt; rule1.rule_id
11.;</description>
		<content:encoded><![CDATA[<p>why not to simplify final query with:</p>
<p>01.UPDATE<br />
02.    rules<br />
03.SET<br />
04.    priority = rule1.priority<br />
05.FROM<br />
06.    rules AS rule1<br />
07.WHERE<br />
08.        rules.rule_id IN (1,4)<br />
09.    AND rule1.rule_id IN (1,4)<br />
10.    AND rules.rule_id &lt;&gt; rule1.rule_id<br />
11.;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Maresa</title>
		<link>http://www.microshell.com/database/sql/swap-values-in-2-rows-sql/comment-page-1/#comment-1440</link>
		<dc:creator>Maresa</dc:creator>
		<pubDate>Wed, 18 Nov 2009 05:15:23 +0000</pubDate>
		<guid isPermaLink="false">http://www.microshell.com/?p=1990#comment-1440</guid>
		<description>Yes ... I realize that th method doesn&#039;t work when there id unique constraint. I&#039;ve tried many ways and I ended up having to do it in multiple statements. :-(</description>
		<content:encoded><![CDATA[<p>Yes &#8230; I realize that th method doesn&#8217;t work when there id unique constraint. I&#8217;ve tried many ways and I ended up having to do it in multiple statements. <img src='http://www.microshell.com/wp-includes/images/smilies/icon_sad.gif' alt=':-(' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: IVO GELOV</title>
		<link>http://www.microshell.com/database/sql/swap-values-in-2-rows-sql/comment-page-1/#comment-1409</link>
		<dc:creator>IVO GELOV</dc:creator>
		<pubDate>Mon, 16 Nov 2009 18:54:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.microshell.com/?p=1990#comment-1409</guid>
		<description>This method does not work when we have unique constraint/index on the given column :(</description>
		<content:encoded><![CDATA[<p>This method does not work when we have unique constraint/index on the given column <img src='http://www.microshell.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Regina</title>
		<link>http://www.microshell.com/database/sql/swap-values-in-2-rows-sql/comment-page-1/#comment-976</link>
		<dc:creator>Regina</dc:creator>
		<pubDate>Thu, 15 Oct 2009 19:50:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.microshell.com/?p=1990#comment-976</guid>
		<description>Small little comment.  I think the PostgreSQL create table statement to more mirror the MySQL is

CREATE TABLE rules (
    rule_id serial PRIMARY KEY,
    rule_name  varchar(255)  NOT NULL,
    priority integer
) ;

the without oids can be left out since its the default behavior for newer versions.</description>
		<content:encoded><![CDATA[<p>Small little comment.  I think the PostgreSQL create table statement to more mirror the MySQL is</p>
<p>CREATE TABLE rules (<br />
    rule_id serial PRIMARY KEY,<br />
    rule_name  varchar(255)  NOT NULL,<br />
    priority integer<br />
) ;</p>
<p>the without oids can be left out since its the default behavior for newer versions.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Onno Molenkamp</title>
		<link>http://www.microshell.com/database/sql/swap-values-in-2-rows-sql/comment-page-1/#comment-952</link>
		<dc:creator>Onno Molenkamp</dc:creator>
		<pubDate>Mon, 12 Oct 2009 10:25:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.microshell.com/?p=1990#comment-952</guid>
		<description>It can be done with a few where-conditions less:

update rules
set priority = rule1.priority
from rules rule1
where rules.rule_id in (1, 4)
and rule1.rule_id + rules.rule_id = 1 + 4;</description>
		<content:encoded><![CDATA[<p>It can be done with a few where-conditions less:</p>
<p>update rules<br />
set priority = rule1.priority<br />
from rules rule1<br />
where rules.rule_id in (1, 4)<br />
and rule1.rule_id + rules.rule_id = 1 + 4;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Maresa</title>
		<link>http://www.microshell.com/database/sql/swap-values-in-2-rows-sql/comment-page-1/#comment-943</link>
		<dc:creator>Maresa</dc:creator>
		<pubDate>Sun, 11 Oct 2009 21:15:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.microshell.com/?p=1990#comment-943</guid>
		<description>The Cartesian product will result a combination of (1, 1), (1,4), (4,1), (4,4). The WHERE condition &quot;rules.rule_id  rule1.rule_id&quot; will get you (1, 4) and (4, 1). So the WHERE condition guarantees distinct, unless I&#039;m missing something.</description>
		<content:encoded><![CDATA[<p>The Cartesian product will result a combination of (1, 1), (1,4), (4,1), (4,4). The WHERE condition &#8220;rules.rule_id  rule1.rule_id&#8221; will get you (1, 4) and (4, 1). So the WHERE condition guarantees distinct, unless I&#8217;m missing something.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

