• 随机查询数据


    要随机查询数据,一开始我们可能想到最简单的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 即可

  • 相关阅读:
    Hibernatede 一对多映射配置
    Hibrenate之事务的理解以及代码编写
    The servlet name already exists.解决方法
    hibernate入门程序
    什么是orm思想?
    Java的MVC模式简介
    JAVA框架之Hibernate框架的学习步骤
    java常见命名规则
    解决get方法提交参数中文乱码问题:
    谈谈对Spring IOC(控制反转)的理解--转
  • 原文地址:https://www.cnblogs.com/feng18/p/5268726.html
Copyright © 2020-2023  润新知