<?xml version="1.0" encoding="utf-8" ?><rss version="2.0" xml:base="https://www.webmaster-forums.net/crss/node/1005910" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title></title>
    <link>https://www.webmaster-forums.net/crss/node/1005910</link>
    <description></description>
    <language>en</language>
          <item>
    <title></title>
    <link>https://www.webmaster-forums.net/web-database-development/finding-lowest-values-database#comment-1031518</link>
    <description> &lt;p&gt;Actually, this isn&#039;t exactly correct. The reason is that it does not take ties into account; the 10th entry may have the exact same value as the 11th entry in this case, and you&#039;d never know it.&lt;/p&gt;
&lt;p&gt;A different way to do this is with the following SQL (courtesy of Joe Celko&#039;s SQL for Smarties book):&lt;/p&gt;
&lt;p&gt;select distinct count(*) , a.salary&lt;br /&gt;
from employees A, employees B&lt;br /&gt;
where a.salary &amp;lt;= b.salary&lt;br /&gt;
group by a.salary&lt;br /&gt;
having count(*) &amp;lt;= 10&lt;/p&gt;
&lt;p&gt;The problem with this SQL is that since it is essentially cartesian-joining two tables together, it doesn&#039;t work too well with huge tables. The concept is that each row in your table will be joined to every row in the table that has a salary less than or equal to the first row. Your #1 salary will be joined to itself (having a count(*) of 1), the #2 salary will be joined to the #1 salary and itself (having a count(*) of 2), etc. The HAVING clause limits your rows to the top 10.&lt;/p&gt;
&lt;p&gt;The best part of this is that it doesn&#039;t use sub-selects (mySQL can&#039;t handle them) but it can be written with sub-selects also. It doesn&#039;t perform any better, but is a bit more clear:&lt;/p&gt;
&lt;p&gt;SELECT DISTINCT salary&lt;br /&gt;
  FROM Personnel AS P1&lt;br /&gt;
  WHERE 10 &amp;gt;= (SELECT COUNT(*)&lt;br /&gt;
              FROM Personnel AS P2&lt;br /&gt;
              WHERE P1.salary &amp;lt; P2.salary)&lt;/p&gt;
&lt;p&gt;Ralph&lt;br /&gt;
 &lt;a href=&quot;http://www.hockeydb.com&quot; class=&quot;bb-url&quot;&gt;http://www.hockeydb.com&lt;/a&gt;&lt;/p&gt;
 </description>
     <pubDate>Tue, 02 May 2000 13:01:00 +0000</pubDate>
 <dc:creator>Ralph Slate</dc:creator>
 <guid isPermaLink="false">comment 1031518 at https://www.webmaster-forums.net</guid>
  </item>
  <item>
    <title></title>
    <link>https://www.webmaster-forums.net/web-database-development/finding-lowest-values-database#comment-1031517</link>
    <description> &lt;p&gt;Thanks a lot.&lt;/p&gt;
&lt;p&gt;I have it working now using:&lt;br /&gt;
$num = 10;&lt;br /&gt;
$query=&quot;SELECT * FROM $table ORDER BY SCORE ASC LIMIT $num&quot;;&lt;br /&gt;
$mysql_result = mysql_query($query);&lt;/p&gt;
&lt;p&gt;------------------&lt;br /&gt;
&lt;a href=&quot;http://www.dlo.net/~rob&quot; class=&quot;bb-url&quot;&gt;Personal Site&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://www.wiredstart.com&quot; class=&quot;bb-url&quot;&gt;http://www.wiredstart.com&lt;/a&gt;  : The Technology Start Page&lt;/p&gt;
 </description>
     <pubDate>Sun, 30 Apr 2000 18:53:00 +0000</pubDate>
 <dc:creator>Rob Pengelly</dc:creator>
 <guid isPermaLink="false">comment 1031517 at https://www.webmaster-forums.net</guid>
  </item>
  <item>
    <title></title>
    <link>https://www.webmaster-forums.net/web-database-development/finding-lowest-values-database#comment-1031516</link>
    <description> &lt;p&gt;robp,&lt;/p&gt;
&lt;p&gt;If you&#039;re using ADO you can simply use the MaxRecords property &amp;amp; ORDER BY command:&lt;/p&gt;
&lt;p&gt;&amp;lt;%&lt;br /&gt;
Set Conn = Server.CreateObject(&quot;ADODB.Connection&quot;)&lt;br /&gt;
Conn.Open &quot;DSN=YourDSN;UID=YourID;PWD=YourPW&quot;&lt;br /&gt;
Set RSLowScores = Server.CreateObject(&quot;ADODB.Recordset&quot;)&lt;br /&gt;
RSLowScores.MaxRecords = 10&lt;br /&gt;
SQL = &quot;SELECT * FROM YourTable &quot;&lt;br /&gt;
SQL = SQL &amp;amp; &quot;ORDER BY score ASC&quot;&lt;br /&gt;
RSLowScores.Open SQL,Conn,1,2&lt;br /&gt;
%&amp;gt;&lt;/p&gt;
&lt;p&gt;That will just return the 10 lowest scores. If you&#039;re not using ADO you can still achieve the same results with a bit more work:&lt;/p&gt;
&lt;p&gt;&amp;lt;%&lt;br /&gt;
Set Conn = Server.CreateObject(&quot;ADODB.Connection&quot;)&lt;br /&gt;
Conn.Open &quot;DSN=YourDSN;UID=YourID;PWD=YourPW&quot;&lt;/p&gt;
&lt;p&gt;SQL = &quot;SELECT * FROM YourTable&quot;&lt;br /&gt;
SQL = SQL &amp;amp; &quot;ORDER BY score ASC&quot;&lt;/p&gt;
&lt;p&gt;Set GetLowestScores = Conn.Execute(SQL)&lt;/p&gt;
&lt;p&gt;&#039; Do a loop until we reach 10&lt;br /&gt;
Dim MaximumRecord&lt;br /&gt;
MaximumRecord = 0&lt;br /&gt;
Do while MaximumRecord &amp;lt;&amp;gt; 10&lt;br /&gt;
%&amp;gt;&lt;br /&gt;
Your db results go here&lt;br /&gt;
&amp;lt;% MaximumRecord = MaximumRecord + 1&lt;br /&gt;
GetLowestScore.movenext&lt;br /&gt;
loop %&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;% &#039;Loop Done&lt;br /&gt;
Set GetLowestScore = nothing %&amp;gt;&lt;/p&gt;
&lt;p&gt;That should do the trick, you would obviously have to add error checking.&lt;/p&gt;
&lt;p&gt;Forgot to mention this example is ASP &amp;amp; SQL 7.  &lt;img src=&quot;http://www.webmaster-forums.com/ubb/smile.gif&quot; alt=&quot;&quot; class=&quot;bb-image&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Regards,&lt;br /&gt;
Peter J. Boettcher&lt;/p&gt;
&lt;p&gt;[This message has been edited by Peter J. Boettcher (edited 30 April 2000).]&lt;/p&gt;
 </description>
     <pubDate>Sun, 30 Apr 2000 17:00:00 +0000</pubDate>
 <dc:creator>Peter J. Boettcher</dc:creator>
 <guid isPermaLink="false">comment 1031516 at https://www.webmaster-forums.net</guid>
  </item>
  </channel>
</rss>
