• 技术文章 >后端开发 >php教程

    求查询优化步骤

    2016-06-13 11:50:08原创723
    求查询优化方法
    请问下面的代码有优化的方法吗



    $catalog_1 = "select name from commodity where catalog = 1 ORDER BY rank desc LIMIT 10";

    $result_1 = mysql_query($catalog_1);

    while($row_1 = mysql_fetch_assoc($result_1)){
    $response [] = $row_1;
    }

    $catalog_2 = "select name from commodity where catalog = 2 ORDER BY rank desc LIMIT 10";

    $result_2 = mysql_query($catalog_2);

    while($row_2 = mysql_fetch_assoc($result_2)){
    $response [] = $row_2;
    }

    $catalog_3 = "select name from commodity where catalog = 3 ORDER BY rank desc LIMIT 10";

    $result_3 = mysql_query($catalog_3);

    while($row_3 = mysql_fetch_assoc($result_3)){
    $response [] = $row_3;
    }

    $catalog_4 = "select name from commodity where catalog = 4 ORDER BY rank desc LIMIT 10";

    $result_4 = mysql_query($catalog_4);

    while($row_4 = mysql_fetch_assoc($result_4)){
    $response [] = $row_4;
    }

    $catalog_5 = "select name from commodity where catalog = 5 ORDER BY rank desc LIMIT 10";

    $result_5 = mysql_query($catalog_5);

    while($row_5 = mysql_fetch_assoc($result_5)){
    $response [] = $row_5;
    }

    $catalog_6 = "select name from commodity where catalog = 6 ORDER BY rank desc LIMIT 10";

    $result_6 = mysql_query($catalog_6);

    while($row_6 = mysql_fetch_assoc($result_6)){
    $response [] = $row_6;
    }

    $catalog_7 = "select name from commodity where catalog = 7 ORDER BY rank desc LIMIT 10";

    $result_7 = mysql_query($catalog_7);

    while($row_7 = mysql_fetch_assoc($result_7)){
    $response [] = $row_7;
    }

    $catalog_8 = "select name from commodity where catalog = 8 ORDER BY rank desc LIMIT 10";

    $result_8 = mysql_query($catalog_8);

    while($row_8 = mysql_fetch_assoc($result_8)){
    $response [] = $row_8;
    }

    $catalog_11 = "select name from commodity where catalog = 11 ORDER BY rank desc LIMIT 10";

    $result_11 = mysql_query($catalog_11);

    while($row_11 = mysql_fetch_assoc($result_11)){
    $response [] = $row_11;
    }

    $catalog_10 = "select name from commodity where catalog = 10 ORDER BY rank desc LIMIT 10";

    $result_10 = mysql_query($catalog_10);

    while($row_10 = mysql_fetch_assoc($result_10)){
    $response [] = $row_10;
    }


    ------解决方案--------------------
    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()){
    $catalog = "select name from commodity where catalog = ".$catalog." ORDER BY rank desc LIMIT 10";
    $result = mysql_query($catalog);

    while($row = mysql_fetch_assoc($result)){
    $response [] = $row;
    }
    }
    $response = array();
    getCataLog(1,$response);
    var_dump($response);
    getCataLog(2,$response);
    var_dump($response);
    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    上一篇:请问IP地址url重写的有关问题 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • 系统讲解PHP缓存技术• 一文理解和实现现代PHP框架里的IOC容器• 深入了解PHP:用gdb调试源码• 实例介绍:使用Docker快速搭建多版本PHP开发环境• 实例详解PHP统计代码行数及文件数量的方法
    1/1

    PHP中文网