<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.2.2" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: How to find next and previous records in SQL</title>
	<link>http://www.xaprb.com/blog/2006/04/28/how-to-find-next-and-previous-records-in-sql/</link>
	<description>Stay curious!</description>
	<pubDate>Sat, 30 Aug 2008 04:28:43 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.2</generator>

	<item>
		<title>By: Anthony</title>
		<link>http://www.xaprb.com/blog/2006/04/28/how-to-find-next-and-previous-records-in-sql/#comment-14522</link>
		<author>Anthony</author>
		<pubDate>Tue, 06 May 2008 18:22:34 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/04/28/how-to-find-next-and-previous-records-in-sql/#comment-14522</guid>
		<description>To: soid

Thanks! I see the problem. I think what I need instead of a LEFT JOIN, is a FULL OUTER JOIN. The corrected query would be...

SELECT table.*, next.id AS next_id, prev.id AS prev_id
FROM table
FULL OUTER JOIN table AS next ON next.position &#62; table.position
FULL OUTER JOIN table AS prev ON prev.position &#60; table.position
WHERE table.id = ?
ORDER BY position ASC, prev_id DESC LIMIT 1;

Note: This is UNTESTED...</description>
		<content:encoded><![CDATA[<p>To: soid</p>
<p>Thanks! I see the problem. I think what I need instead of a LEFT JOIN, is a FULL OUTER JOIN. The corrected query would be&#8230;</p>
<p>SELECT table.*, next.id AS next_id, prev.id AS prev_id<br />
FROM table<br />
FULL OUTER JOIN table AS next ON next.position &gt; table.position<br />
FULL OUTER JOIN table AS prev ON prev.position &lt; table.position<br />
WHERE table.id = ?<br />
ORDER BY position ASC, prev_id DESC LIMIT 1;</p>
<p>Note: This is UNTESTED&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: soid</title>
		<link>http://www.xaprb.com/blog/2006/04/28/how-to-find-next-and-previous-records-in-sql/#comment-14521</link>
		<author>soid</author>
		<pubDate>Tue, 06 May 2008 14:06:56 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/04/28/how-to-find-next-and-previous-records-in-sql/#comment-14521</guid>
		<description>To Xaprb

I have MySql of 5.0.44 version and your method doesn't work. So I found the error when I make a query like this:

SELECT t1, sign(t1-100), t1-100 FROM table

And I received something like that:

98  &#124; 1 &#124; 18446744073709551613
99  &#124; 1 &#124; 18446744073709551612
101 &#124; 1 &#124; 1
102 &#124; 1 &#124; 2
..etc..

So I solved the problem when I take off an "unsigned" flag for the id column (sh*t, MySql Query Browser uses it by default!).
And so I see X-Crap has a similar problem when he creates table: 

CREATE TABLE `acores_imagens` (
`imgid` mediumint(6) unsigned NOT NULL auto_increment,</description>
		<content:encoded><![CDATA[<p>To Xaprb</p>
<p>I have MySql of 5.0.44 version and your method doesn&#8217;t work. So I found the error when I make a query like this:</p>
<p>SELECT t1, sign(t1-100), t1-100 FROM table</p>
<p>And I received something like that:</p>
<p>98  | 1 | 18446744073709551613<br />
99  | 1 | 18446744073709551612<br />
101 | 1 | 1<br />
102 | 1 | 2<br />
..etc..</p>
<p>So I solved the problem when I take off an &#8220;unsigned&#8221; flag for the id column (sh*t, MySql Query Browser uses it by default!).<br />
And so I see X-Crap has a similar problem when he creates table: </p>
<p>CREATE TABLE `acores_imagens` (<br />
`imgid` mediumint(6) unsigned NOT NULL auto_increment,</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: soid</title>
		<link>http://www.xaprb.com/blog/2006/04/28/how-to-find-next-and-previous-records-in-sql/#comment-14520</link>
		<author>soid</author>
		<pubDate>Tue, 06 May 2008 13:21:19 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/04/28/how-to-find-next-and-previous-records-in-sql/#comment-14520</guid>
		<description>To Anthony Altemara

Your query doesn't work if we use it for last element. Query returns just empty :-(</description>
		<content:encoded><![CDATA[<p>To Anthony Altemara</p>
<p>Your query doesn&#8217;t work if we use it for last element. Query returns just empty :-(</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anthony Altemara</title>
		<link>http://www.xaprb.com/blog/2006/04/28/how-to-find-next-and-previous-records-in-sql/#comment-14369</link>
		<author>Anthony Altemara</author>
		<pubDate>Sun, 30 Mar 2008 19:23:06 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/04/28/how-to-find-next-and-previous-records-in-sql/#comment-14369</guid>
		<description>Ok... I'll try encoding the &#62;'s and &#60;'s....

SELECT table.*, next.id AS next_id, prev.id AS prev_id 
FROM table 
LEFT JOIN table AS next ON next.position &#62; table.position 
LEFT JOIN table AS prev ON prev.position &#60; table.position 
WHERE table.id = ? 
ORDER BY position ASC, prev_id DESC LIMIT 1;</description>
		<content:encoded><![CDATA[<p>Ok&#8230; I&#8217;ll try encoding the &gt;&#8217;s and &lt;&#8217;s&#8230;.</p>
<p>SELECT table.*, next.id AS next_id, prev.id AS prev_id<br />
FROM table<br />
LEFT JOIN table AS next ON next.position &gt; table.position<br />
LEFT JOIN table AS prev ON prev.position &lt; table.position<br />
WHERE table.id = ?<br />
ORDER BY position ASC, prev_id DESC LIMIT 1;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anthony Altemara</title>
		<link>http://www.xaprb.com/blog/2006/04/28/how-to-find-next-and-previous-records-in-sql/#comment-14368</link>
		<author>Anthony Altemara</author>
		<pubDate>Sun, 30 Mar 2008 19:15:59 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/04/28/how-to-find-next-and-previous-records-in-sql/#comment-14368</guid>
		<description>Darn! I swear I keep posting the complete query....


SELECT table.*, next.id AS next_id, prev.id AS prev_id 
FROM table 
LEFT JOIN table AS next ON next.position &#62; table.position
LEFT JOIN table AS prev ON prev.position </description>
		<content:encoded><![CDATA[<p>Darn! I swear I keep posting the complete query&#8230;.</p>
<p>SELECT table.*, next.id AS next_id, prev.id AS prev_id<br />
FROM table<br />
LEFT JOIN table AS next ON next.position &gt; table.position<br />
LEFT JOIN table AS prev ON prev.position</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loughlin McSweeney</title>
		<link>http://www.xaprb.com/blog/2006/04/28/how-to-find-next-and-previous-records-in-sql/#comment-14367</link>
		<author>Loughlin McSweeney</author>
		<pubDate>Sun, 30 Mar 2008 18:53:46 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/04/28/how-to-find-next-and-previous-records-in-sql/#comment-14367</guid>
		<description>Brilliant solution - clever SQL, very nice!</description>
		<content:encoded><![CDATA[<p>Brilliant solution - clever SQL, very nice!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anthony Altemara</title>
		<link>http://www.xaprb.com/blog/2006/04/28/how-to-find-next-and-previous-records-in-sql/#comment-14252</link>
		<author>Anthony Altemara</author>
		<pubDate>Tue, 26 Feb 2008 18:10:45 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/04/28/how-to-find-next-and-previous-records-in-sql/#comment-14252</guid>
		<description>correction on my last post... The query is incorrect. It will always return the first record as the prev_id... I had to add another ORDER BY on the end

Here's the updated query:

SELECT table.*, next.id AS next_id, prev.id AS prev_id 
  FROM table 
  LEFT JOIN table AS next ON next.position &#62; table.position
  LEFT JOIN table AS prev ON prev.position </description>
		<content:encoded><![CDATA[<p>correction on my last post&#8230; The query is incorrect. It will always return the first record as the prev_id&#8230; I had to add another ORDER BY on the end</p>
<p>Here&#8217;s the updated query:</p>
<p>SELECT table.*, next.id AS next_id, prev.id AS prev_id<br />
  FROM table<br />
  LEFT JOIN table AS next ON next.position &gt; table.position<br />
  LEFT JOIN table AS prev ON prev.position</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anthony Altemara</title>
		<link>http://www.xaprb.com/blog/2006/04/28/how-to-find-next-and-previous-records-in-sql/#comment-14251</link>
		<author>Anthony Altemara</author>
		<pubDate>Tue, 26 Feb 2008 17:40:21 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/04/28/how-to-find-next-and-previous-records-in-sql/#comment-14251</guid>
		<description>I've done something similiar with:

SELECT table.*, next.id AS next_id, prev.id AS prev_id
 FROM table 
 LEFT JOIN table AS next ON next.position &#62; table.position 
 LEFT JOIN table AS prev ON prev.position &#60; table.position  
 WHERE table.id = 21 
 ORDER BY position LIMIT 1;</description>
		<content:encoded><![CDATA[<p>I&#8217;ve done something similiar with:</p>
<p>SELECT table.*, next.id AS next_id, prev.id AS prev_id<br />
 FROM table<br />
 LEFT JOIN table AS next ON next.position &gt; table.position<br />
 LEFT JOIN table AS prev ON prev.position &lt; table.position<br />
 WHERE table.id = 21<br />
 ORDER BY position LIMIT 1;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Almanca tercüman</title>
		<link>http://www.xaprb.com/blog/2006/04/28/how-to-find-next-and-previous-records-in-sql/#comment-14165</link>
		<author>Almanca tercüman</author>
		<pubDate>Tue, 15 Jan 2008 15:33:43 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/04/28/how-to-find-next-and-previous-records-in-sql/#comment-14165</guid>
		<description>In which place? Can you rewrite the query or part of the query and get the same results? If it’s simpler, I’m all for it...</description>
		<content:encoded><![CDATA[<p>In which place? Can you rewrite the query or part of the query and get the same results? If it’s simpler, I’m all for it&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: &#124;X-Crap&#124;</title>
		<link>http://www.xaprb.com/blog/2006/04/28/how-to-find-next-and-previous-records-in-sql/#comment-14115</link>
		<author>&#124;X-Crap&#124;</author>
		<pubDate>Fri, 28 Dec 2007 16:58:16 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/04/28/how-to-find-next-and-previous-records-in-sql/#comment-14115</guid>
		<description>Argh.. the script cuts me alot of code..
Here is the really correct code i used:

http://www.acores.net/code.txt</description>
		<content:encoded><![CDATA[<p>Argh.. the script cuts me alot of code..<br />
Here is the really correct code i used:</p>
<p><a href="http://www.acores.net/code.txt" rel="nofollow">http://www.acores.net/code.txt</a></p>
]]></content:encoded>
	</item>
</channel>
</rss>
