<?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: Next/Previous with MySQL</title>
	<atom:link href="http://jystewart.net/process/2005/06/nextprevious-with-mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://jystewart.net/process/2005/06/nextprevious-with-mysql/</link>
	<description>notes from another web developer</description>
	<lastBuildDate>Sat, 24 Jul 2010 07:17:45 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: James Stewart</title>
		<link>http://jystewart.net/process/2005/06/nextprevious-with-mysql/comment-page-1/#comment-113630</link>
		<dc:creator>James Stewart</dc:creator>
		<pubDate>Sun, 07 Dec 2008 00:48:58 +0000</pubDate>
		<guid isPermaLink="false">http://jystewart.net/process/?p=76#comment-113630</guid>
		<description>resist today art - seems like you probably ought to read up on SQL injection attacks. It&#039;s a very bad idea to use string concatenation to build SQL queries, and especially bad if the strings being concatenated are from user input.

http://en.wikipedia.org/wiki/SQL_injection</description>
		<content:encoded><![CDATA[<p>resist today art &#8211; seems like you probably ought to read up on SQL injection attacks. It&#8217;s a very bad idea to use string concatenation to build SQL queries, and especially bad if the strings being concatenated are from user input.</p>
<p><a href="http://en.wikipedia.org/wiki/SQL_injection" rel="nofollow">http://en.wikipedia.org/wiki/SQL_injection</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: resist today art</title>
		<link>http://jystewart.net/process/2005/06/nextprevious-with-mysql/comment-page-1/#comment-113269</link>
		<dc:creator>resist today art</dc:creator>
		<pubDate>Thu, 04 Dec 2008 03:24:15 +0000</pubDate>
		<guid isPermaLink="false">http://jystewart.net/process/?p=76#comment-113269</guid>
		<description>James &#8212; you bet! $album is part of the query string so something like $album = &#039;typography&#039;</description>
		<content:encoded><![CDATA[<p>James &mdash; you bet! $album is part of the query string so something like $album = &#8216;typography&#8217;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nishi</title>
		<link>http://jystewart.net/process/2005/06/nextprevious-with-mysql/comment-page-1/#comment-112354</link>
		<dc:creator>Nishi</dc:creator>
		<pubDate>Thu, 27 Nov 2008 21:20:37 +0000</pubDate>
		<guid isPermaLink="false">http://jystewart.net/process/?p=76#comment-112354</guid>
		<description>Great! This query is great. However, I&#039;ve run into a problem that I am hoping someone will be able to help me with.

CREATE TABLE t ( id TINYINT, title VARCHAR(10) );
INSERT INTO t (id, title) VALUES (1, &#039;aaa&#039;), (2, &#039;bbb&#039;), (3, &#039;ccc&#039;), (4, &#039;ddd&#039;), (5, &#039;eee&#039;);

(a) DO @sn := 0;
SELECT @sn:=@sn+1 sn, id, title FROM t GROUP BY id ORDER BY id desc;
&#124; sn   &#124; id   &#124; title &#124;
+------+------+-------+
&#124;    1 &#124;    5 &#124; eee   &#124; 
&#124;    2 &#124;    4 &#124; ddd   &#124; 
&#124;    3 &#124;    3 &#124; ccc   &#124; 
&#124;    4 &#124;    2 &#124; bbb   &#124; 
&#124;    5 &#124;    1 &#124; aaa   &#124; 

(b) DO @sn := 0;
SELECT @sn:=@sn+1 sn, id, title FROM t GROUP BY id HAVING sn=2 ORDER BY id desc;
&#124;    2 &#124;    2 &#124; bbb   &#124; 

(c) DO @sn := 0;
SELECT @sn:=@sn+1 sn, id, title FROM t HAVING sn=2 ORDER BY id desc;
&#124;    3 &#124;    4 &#124; ddd   &#124; 

I do not understand why (b) is not able to retain the same order of sn as in (a). I am assuming it is the &#039;HAVING sn=2&#039; that is forcing (b) to reset the sort order. However, (c) is able to maintain the same sn order as in (a) even with &#039;HAVING sn=2&#039; clause in it, although it is not using the &#039;GROUP BY id&#039; anymore.

Is there a way I can retain the sn sort order in (b) same as in (a)?

&#124; version() &#124;
+-----------+
&#124; 5.0.51a   &#124;</description>
		<content:encoded><![CDATA[<p>Great! This query is great. However, I&#8217;ve run into a problem that I am hoping someone will be able to help me with.</p>
<p>CREATE TABLE t ( id TINYINT, title VARCHAR(10) );<br />
INSERT INTO t (id, title) VALUES (1, &#8216;aaa&#8217;), (2, &#8216;bbb&#8217;), (3, &#8216;ccc&#8217;), (4, &#8216;ddd&#8217;), (5, &#8216;eee&#8217;);</p>
<p>(a) DO @sn := 0;<br />
SELECT @sn:=@sn+1 sn, id, title FROM t GROUP BY id ORDER BY id desc;<br />
| sn   | id   | title |<br />
+&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;-+<br />
|    1 |    5 | eee   |<br />
|    2 |    4 | ddd   |<br />
|    3 |    3 | ccc   |<br />
|    4 |    2 | bbb   |<br />
|    5 |    1 | aaa   | </p>
<p>(b) DO @sn := 0;<br />
SELECT @sn:=@sn+1 sn, id, title FROM t GROUP BY id HAVING sn=2 ORDER BY id desc;<br />
|    2 |    2 | bbb   | </p>
<p>(c) DO @sn := 0;<br />
SELECT @sn:=@sn+1 sn, id, title FROM t HAVING sn=2 ORDER BY id desc;<br />
|    3 |    4 | ddd   | </p>
<p>I do not understand why (b) is not able to retain the same order of sn as in (a). I am assuming it is the &#8216;HAVING sn=2&#8242; that is forcing (b) to reset the sort order. However, (c) is able to maintain the same sn order as in (a) even with &#8216;HAVING sn=2&#8242; clause in it, although it is not using the &#8216;GROUP BY id&#8217; anymore.</p>
<p>Is there a way I can retain the sn sort order in (b) same as in (a)?</p>
<p>| version() |<br />
+&#8212;&#8212;&#8212;&#8211;+<br />
| 5.0.51a   |</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: James Stewart</title>
		<link>http://jystewart.net/process/2005/06/nextprevious-with-mysql/comment-page-1/#comment-112281</link>
		<dc:creator>James Stewart</dc:creator>
		<pubDate>Thu, 27 Nov 2008 08:21:23 +0000</pubDate>
		<guid isPermaLink="false">http://jystewart.net/process/?p=76#comment-112281</guid>
		<description>Glad it&#039;s helpful, resist today art. I trust you&#039;re actually using some sort of parameter substitution/prepared queries and aren&#039;t really using string concatenation to construct your queries?</description>
		<content:encoded><![CDATA[<p>Glad it&#8217;s helpful, resist today art. I trust you&#8217;re actually using some sort of parameter substitution/prepared queries and aren&#8217;t really using string concatenation to construct your queries?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: resist today art</title>
		<link>http://jystewart.net/process/2005/06/nextprevious-with-mysql/comment-page-1/#comment-112134</link>
		<dc:creator>resist today art</dc:creator>
		<pubDate>Wed, 26 Nov 2008 15:07:30 +0000</pubDate>
		<guid isPermaLink="false">http://jystewart.net/process/?p=76#comment-112134</guid>
		<description>Ace, this query is awesome! I&#039;m using it for a photo gallery in codeigniter and it&#039;s perfect for getting the next/prev IDs for links.

Here&#039;s my code, I hope someone finds it useful:

SELECT id, title, @a := id, ( SELECT id FROM art WHERE album = &#039;$album&#039; AND id  @a ORDER BY title ASC LIMIT 1 ) AS next_id FROM art WHERE album = &#039;$album&#039; AND id = &#039;$id&#039;</description>
		<content:encoded><![CDATA[<p>Ace, this query is awesome! I&#8217;m using it for a photo gallery in codeigniter and it&#8217;s perfect for getting the next/prev IDs for links.</p>
<p>Here&#8217;s my code, I hope someone finds it useful:</p>
<p>SELECT id, title, @a := id, ( SELECT id FROM art WHERE album = &#8216;$album&#8217; AND id  @a ORDER BY title ASC LIMIT 1 ) AS next_id FROM art WHERE album = &#8216;$album&#8217; AND id = &#8216;$id&#8217;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ace suares</title>
		<link>http://jystewart.net/process/2005/06/nextprevious-with-mysql/comment-page-1/#comment-36348</link>
		<dc:creator>ace suares</dc:creator>
		<pubDate>Tue, 10 Apr 2007 01:16:40 +0000</pubDate>
		<guid isPermaLink="false">http://jystewart.net/process/?p=76#comment-36348</guid>
		<description>Seems that the formatting did something to my statement, so here we go again:

I made up the follwing:

SELECT id, title, @a := title, ( SELECT id FROM table WHERE title &lt; @a ORDER BY title DESC LIMIT 1 ) AS prev_id,
( SELECT id FROM table WHERE title &gt; @a ORDER BY title ASC LIMIT 1 ) AS next_id

prev_id and next_id will hold the id of the previous and next record based on the sort criteria (title). If there is nothing before or after, then they will be empty...

Hope this helps...</description>
		<content:encoded><![CDATA[<p>Seems that the formatting did something to my statement, so here we go again:</p>
<p>I made up the follwing:</p>
<p>SELECT id, title, @a := title, ( SELECT id FROM table WHERE title &lt; @a ORDER BY title DESC LIMIT 1 ) AS prev_id,<br />
( SELECT id FROM table WHERE title &gt; @a ORDER BY title ASC LIMIT 1 ) AS next_id</p>
<p>prev_id and next_id will hold the id of the previous and next record based on the sort criteria (title). If there is nothing before or after, then they will be empty&#8230;</p>
<p>Hope this helps&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ace suares</title>
		<link>http://jystewart.net/process/2005/06/nextprevious-with-mysql/comment-page-1/#comment-36347</link>
		<dc:creator>ace suares</dc:creator>
		<pubDate>Tue, 10 Apr 2007 01:14:27 +0000</pubDate>
		<guid isPermaLink="false">http://jystewart.net/process/?p=76#comment-36347</guid>
		<description>I made up the follwing:

SELECT id, title, @a := title, ( SELECT id FROM table WHERE title  @a ORDER BY title ASC LIMIT 1 ) AS next_id

prev_id and next_id will hold the id of the previous and next record based on the sort criteria (title). If there is nothing before or after, then they will be empty...

Hope this helps...

ace</description>
		<content:encoded><![CDATA[<p>I made up the follwing:</p>
<p>SELECT id, title, @a := title, ( SELECT id FROM table WHERE title  @a ORDER BY title ASC LIMIT 1 ) AS next_id</p>
<p>prev_id and next_id will hold the id of the previous and next record based on the sort criteria (title). If there is nothing before or after, then they will be empty&#8230;</p>
<p>Hope this helps&#8230;</p>
<p>ace</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: RaginBajin</title>
		<link>http://jystewart.net/process/2005/06/nextprevious-with-mysql/comment-page-1/#comment-114</link>
		<dc:creator>RaginBajin</dc:creator>
		<pubDate>Tue, 30 Aug 2005 15:49:12 +0000</pubDate>
		<guid isPermaLink="false">http://jystewart.net/process/?p=76#comment-114</guid>
		<description>An easy and quick way in doing this is something like this:

select * from mytable LIMIT 0,20

The Limit 0,20 will give you a listing of 20 entries starting from 0. So, 1-20 records would be displayed. If you keep record of the number where you started in the header or somewhere else, then this would work pretty easy for you. 

select * from mytable LIMIT 30,20  - Start from 30 and given you the next 20 records.</description>
		<content:encoded><![CDATA[<p>An easy and quick way in doing this is something like this:</p>
<p>select * from mytable LIMIT 0,20</p>
<p>The Limit 0,20 will give you a listing of 20 entries starting from 0. So, 1-20 records would be displayed. If you keep record of the number where you started in the header or somewhere else, then this would work pretty easy for you. </p>
<p>select * from mytable LIMIT 30,20  &#8211; Start from 30 and given you the next 20 records.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
