zoukankan      html  css  js  c++  java
  • mysql in(...) 1次取出当前记录及上1条下1条记录, 且使用了主键作为索引

     mysql 1次取出当前记录及上1条下1条记录,效果非常好!

     

    对于让mysql一次取出当前记录,及上1条、下1,目前网上那条sign( ID - 3 )...sql语句反正我是看得云里雾里,

      自己写了一条比较清晰的sql语句,就是select ... in ( 4 , 5 , 6 ) 的语句,其中456通过union 连接起来mysql的任务就完成了。然后用phpmysql给出的数据处理一下,ok

     

      由于才取3条数据, mysql语句怎么union都不会有什么性能问题的。

    -------------------------------------------
     

    一次取出编号为7的记录 和 它的上一条、下一条
    SELECT * FROM article
    where id in(
        SELECT max( id ) FROM article WHERE id < 7 
        union
        select 7
        union
        SELECT min( id ) FROM article WHERE id > 7 
    )
    结果::


     

    技术细节见下:

     

    1.article表结构: aid( 主键 ) title content ... ,

    2.sql语句, 

    3.php执行和处理 .

    <?php

    $id = 5; // 假设http://localhost/mycms/?ac=show&id=5 id5

    $sql = "

           SELECT * FROM article

           where

           `aid` in(

                  SELECT max( `aid` ) FROM article WHERE `aid` < {$id}

                  union all

                  SELECT {$id}

                  union all

                  SELECT min( `aid` ) FROM article WHERE `aid` > {$id} 

           )

           ORDER BY aid ASC

           LIMIT 0,3

    ";

    $list = db::get_all( $sql );

    empty( $list ) && exit( '该文章不存在.' );

     

    // 再次遍历, 判断文章编号是否存在.

    $aid_exists = false;

    $row = array();

    foreach ( $list as $n=>$line ){

           if ( $id == $line['aid'] ){

                  $aid_exists = true;

                  $row = $line; // 当前记录

                  break;

           }

    }     

    false === $aid_exists && exit( '该文章不存在.' );

                 

    // 它的后1

    $row['prev'] = isset( $list[$n-1] ) ? $list[$n-1] : false;

     

    // 它的前1

    $row['next'] = isset( $list[$n+1] ) ? $list[$n+1] : false;

     

    // 载入show.html模版 

    tpl( 'show' , $row );

    ?>

     

    上述得到 $row 的结构:: ( 黄色背景色是当前的数据, prev next分别是上1条、下1 )

    $row = Array(

        [aid] => 5

        [title] => 叶斯算法(bayesian)介绍

        [prev] => Array(

                [aid] => 4

                [title] => ffffffffff

         )

        [next] => Array (

                [aid] => 6

                [title] => 模式四要素

        )

    )

    ----------------

    再在show.html 里面写上如下代码::
    <style>

        #prev_next span{ display: inline-block; 260px ; }

    </style>


    <!--
    1条、后1 -->

    <div id="prev_next">

             <span>

             &lt;下一篇:

             <?php

                       if( $next ) {

                                $next_short = substr( $next['content'] , 0 , 150 );

                                echo "<a href=\"?ac=show&id={$next['aid']}\" title=\"1:{$next['title']}\n\r{$next_short}\" >".substr( $next['title'] , 0 , 30 ).'</a>';

                       }

                       else{

                                echo ' 没有了  ';

                       }

             ?>

             </span>

     

             <span style="text-align:right;">

             上一篇:               

             <?php

                       if( $prev ) {

                                $prev_short = substr( $prev['content'] , 0 , 150 );

                                echo "<a href=\"?ac=show&id={$prev['aid']}\" title=\"1:{$prev['title']}\n\r{$prev_short}\" >".substr( $prev['title'] , 0 , 30 ).'</a>';

                       }

                       else{

                                echo ' 没有了  ';

                       }

             ?>

             &gt;

             </span>

    </div>

     

    结果是相当满意,如下图::

     

     性能分析::

     

    explain一下上面那条语句:
    使用了key: PRIMARY 主键 作为索引,所以性能是可以的.


     

     

  • 相关阅读:
    Path Sum
    Intersection of Two Linked Lists (求两个单链表的相交结点)
    Nginx入门资料
    赛马问题
    翻转单词顺序 VS 左旋转字符串
    重建二叉树
    Fibonacci相关问题
    Find Minimum in Rotated Sorted Array(旋转数组的最小数字)
    常用查找算法总结
    Contains Duplicate
  • 原文地址:https://www.cnblogs.com/wangqishu/p/2734811.html
Copyright © 2011-2022 走看看