求查询优化步骤

WBOY
發布: 2016-06-13 11:50:08
原創
1255 人瀏覽過

求查询优化方法
请问下面的代码有优化的方法吗

<br /><br />$catalog_1 = "select name from commodity where catalog = 1 ORDER BY rank desc LIMIT 10";<br /><br />$result_1 = mysql_query($catalog_1);<br /><br />while($row_1 = mysql_fetch_assoc($result_1)){<br />     $response [] = $row_1;<br />}<br /><br />$catalog_2 = "select name from commodity where catalog = 2 ORDER BY rank desc LIMIT 10";<br /><br />$result_2 = mysql_query($catalog_2);<br /><br />while($row_2 = mysql_fetch_assoc($result_2)){<br />     $response [] = $row_2;<br />}<br /><br />$catalog_3 = "select name from commodity where catalog = 3 ORDER BY rank desc LIMIT 10";<br /><br />$result_3 = mysql_query($catalog_3);<br /><br />while($row_3 = mysql_fetch_assoc($result_3)){<br />     $response [] = $row_3;<br />}<br /><br />$catalog_4 = "select name from commodity where catalog = 4 ORDER BY rank desc LIMIT 10";<br /><br />$result_4 = mysql_query($catalog_4);<br /><br />while($row_4 = mysql_fetch_assoc($result_4)){<br />     $response [] = $row_4;<br />}<br /><br />$catalog_5 = "select name from commodity where catalog = 5 ORDER BY rank desc LIMIT 10";<br /><br />$result_5 = mysql_query($catalog_5);<br /><br />while($row_5 = mysql_fetch_assoc($result_5)){<br />     $response [] = $row_5;<br />}<br /><br />$catalog_6 = "select name from commodity where catalog = 6 ORDER BY rank desc LIMIT 10";<br /><br />$result_6 = mysql_query($catalog_6);<br /><br />while($row_6 = mysql_fetch_assoc($result_6)){<br />     $response [] = $row_6;<br />}<br /><br />$catalog_7 = "select name from commodity where catalog = 7 ORDER BY rank desc LIMIT 10";<br /><br />$result_7 = mysql_query($catalog_7);<br /><br />while($row_7 = mysql_fetch_assoc($result_7)){<br />     $response [] = $row_7;<br />}<br /><br />$catalog_8 = "select name from commodity where catalog = 8 ORDER BY rank desc LIMIT 10";<br /><br />$result_8 = mysql_query($catalog_8);<br /><br />while($row_8 = mysql_fetch_assoc($result_8)){<br />     $response [] = $row_8;<br />}<br /><br />$catalog_11 = "select name from commodity where catalog = 11 ORDER BY rank desc LIMIT 10";<br /><br />$result_11 = mysql_query($catalog_11);<br /><br />while($row_11 = mysql_fetch_assoc($result_11)){<br />     $response [] = $row_11;<br />}<br /><br />$catalog_10 = "select name from commodity where catalog = 10 ORDER BY rank desc LIMIT 10";<br /><br />$result_10 = mysql_query($catalog_10);<br /><br />while($row_10 = mysql_fetch_assoc($result_10)){<br />     $response [] = $row_10;<br />}<br /><br />
登入後複製

------解决方案--------------------
select name,catalog,rank from commodity c where 10 > (select count(*) from commodity where catalog=c.catalog and id>c.id) and catalog in (1,2,3,4,5,6,7,8,10,11) order by c.catalog,c.rank desc
登入後複製

------解决方案--------------------
function getCataLog($catalog,&$response = array()){<br />    $catalog = "select name from commodity where catalog = ".$catalog." ORDER BY rank desc LIMIT 10";<br />    $result = mysql_query($catalog);<br />  <br />    while($row = mysql_fetch_assoc($result)){<br />         $response [] = $row;<br />    }<br />}<br />$response = array();<br />getCataLog(1,$response);<br />var_dump($response);<br />getCataLog(2,$response);<br />var_dump($response);
登入後複製

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板