Discuz! 数据库查询语句书写教程

1.从数据库中取6个值,这些值中的字段可以随意调出。

<!--{eval
   $threads = DB::fetch_all("SELECT * FROM `pre_forum_thread` ORDER BY dateline DESC LIMIT 0,6");
}-->

可以使用loop语句输出这些由字段组成的值。

<!--{loop $threads $value}-->
<p class="news">
     <em>{echo date('d日',$value[dateline])} | </em>
     <a href="t$value[tid].html">$value[subject]</a>
</p>
<!--{/loop}-->

2.增加筛选条件:取pre_portal_article_title表中 catid 介于50到59之间,或66到74之间的值,然后按照时间倒序排列。

<!--{eval
  $app = DB::fetch_all("SELECT * FROM `pre_portal_article_title` WHERE catid BETWEEN 50 AND 59 OR catid BETWEEN 66 AND 74 ORDER BY dateline DESC LIMIT 0,9");
}-->

3.另一种数据库查询方法

通过这种查询获得的变量可以在其作用范围内直接使用。

<!--{eval 
$usernum = DB::result_first("SELECT count(*) FROM ".DB::table('common_member'));
$onlinenum = DB::result_first("SELECT count(*) FROM ".DB::table('common_session'));
$todayposts = DB::result_first("SELECT sum(todayposts) FROM ".DB::table('forum_forum')." WHERE status=3");
$yesterdayposts = DB::result_first("SELECT sum(yesterdayposts) FROM ".DB::table('forum_forum')." WHERE status=3");
$threads = DB::result_first("SELECT sum(threads) FROM ".DB::table('forum_forum')." WHERE status=3");$posts = DB::result_first("SELECT sum(posts) FROM ".DB::table('forum_forum')." WHERE status=3");
$groups = DB::result_first("SELECT count(*) FROM ".DB::table('forum_forum')." WHERE type='sub'");
$record = DB::result_first("SELECT svalue FROM ".DB::table('common_setting')." WHERE skey='onlinerecord'");
$maxrecord = explode(" ",$record);}-->
    原文作者:Microant
    原文地址: https://www.jianshu.com/p/b736272808c1
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞