zoukankan      html  css  js  c++  java
  • 随机查询数据

    要随机查询数据,一开始我们可能想到最简单的order by rand() 来操作,如果生成的页面很多,效率不敢恭维啊,怎么做才能提高效率,现提供代码给各位鞋童参考一哈

    如果指定了栏目,一般最简单的做法是

    {pc:content action="lists" catid="2" num="10" order="rand()"}
    {loop $data $r}
    <li><a href="{$r[url]}" target="_blank"{title_style($r[style])}>{$r[title]}</a> {date('Y-m-d H:i:s',$r[inputtime])}</li>
    {/loop}
    {/pc}

    或者

    {pc:get sql="select * from phpcms_news where status='99' catid in(1,2,3) order by rand()" num="10"}

    刚刚说了,这样的效率很低


    高效率代码:(以文章模型为例)

    {pc:get sql="select * from phpcms_news as t1 join (select round(rand() * ((select max(id) from phpcms_news)-(select min(id) from phpcms_news))+(select min(id) from phpcms_news)) as id) as t2  where t1.id >= t2.id and t1.status=99 order by t1.id" num="10"}


    如果要指定栏目,则在 “order by t1.id” 前面加上 and t1.catid in(1,2,3)

    {pc:get sql="select * from phpcms_news as t1 join (select round(rand() * ((select max(id) from phpcms_news)-(select min(id) from phpcms_news))+(select min(id) from phpcms_news)) as id) as t2  where t1.id >= t2.id and t1.status=99 and t1.catid in(1,2,3) order by t1.id" num="10"}

    如果需要调用当前栏目(包含子栏目)

    {php $bcatid = $CATEGORYS[$catid]['arrchildid'];}
    {pc:get sql="select * from phpcms_news as t1 join (select round(rand() * ((select max(id) from phpcms_news)-(select min(id) from phpcms_news))+(select min(id) from phpcms_news)) as id) as t2  where t1.id >= t2.id and t1.status=99 and t1.catid in($bcatid) order by t1.id" num="10"}

    这里的数据库前缀 phpcms_ 不用管,系统会自动替换为你当前的数据库前缀

    如果需要随机调用 下载 模型的,则把  phpcms_news  改为 phpcms_download 即可

  • 相关阅读:
    字符集WideCharToMultiByte
    [HDF]hdf-4.2.6类库的使用
    [GDAL]读取HDF格式的calipso数据
    C#读写BitMap及颜色相乘
    [Slimdx]顶点和索引缓冲,绘制了2个分离的三角形
    [GDAL]写入shp
    几个环境学概念
    MIConvexHull
    几个力学概念
    [转载]如何破解Excel VBA密码
  • 原文地址:https://www.cnblogs.com/feng18/p/5268726.html
Copyright © 2011-2022 走看看