·设为首页收藏本站📧邮箱修改🎁免费下载专区📒收藏夹👽聊天室📱AI智能体
返回列表 发布新帖

Discuz! X 数据查询表 - 常用查询

420 1
发表于 2020-7-7 09:09:28 | 查看全部 阅读模式

马上注册,免费下载更多dz插件网资源。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
调用最新主题
  1. $new=DB::fetch_all("SELECT tid,subject FROM ".DB::table('forum_thread')." ORDER BY `dateline` desc LIMIT 0,1;");
复制代码

调用最新主题封面图
  1. $img=DB::fetch_all("SELECT a.subject,a.tid,b.attachment FROM ".DB::table("forum_thread")." a LEFT JOIN ".DB::table("forum_threadimage")." b on b.tid=a.tid WHERE a.`attachment`=2 ORDER BY a.`dateline` DESC LIMIT 0,5");
复制代码

调用当前登录用户阅读权限
  1. $read=DB::result(DB::query("SELECT b.readaccess FROM ".DB::table('common_member')." a left join ".DB::table('common_usergroup_field')." b on b.groupid=a.groupid WHERE a.`uid`= '$_G[uid]'"));
复制代码

调用常用统计数据
  1. //获取VERSION、RELEASE$version = unserialize($_G['setting']['cloudaddons_newversion']);//获取站长用户名$name = DB::result_first("select username from ".DB::table("common_member")." where uid = 1");//获取建站时间$time = DB::result_first("select regdate from ".DB::table("common_member")." where uid = 1");//获取模板个数$templates = DB::result_first("select count(styleid) from ".DB::table("common_style")."");//获取插件个数$plugins = DB::result_first("select count(pluginid) from ".DB::table("common_plugin")."");//获取会员人数$users = DB::result_first("select count(uid) from ".DB::table("common_member")."");//获取临时会员人数$guests = DB::result_first("select count(conopenid) from ".DB::table("common_connect_guest")."");//获取会员总人数$all_users = $users + $guests;//获取管理团队人数$admins = DB::result_first("select count(uid) from ".DB::table("common_member")." where groupid in (1,2)");//获取最新注册会员$newuser = DB::result_first("select username from ".DB::table("common_member")." order BY regdate desc");//获取发帖会员人数$post_posts = DB::result_first("select count(uid) from ".DB::table("common_member_count")." where posts > 0 ");//获取发表主题会员人数$post_threads = DB::result_first("select count(uid) from ".DB::table("common_member_count")." where threads > 0 ");//获取版块数$forums = DB::result_first("select count(fid) from ".DB::table("forum_forum")." where type = 'forum' and status in (0,1)");//获取总帖数$posts = DB::result_first("select count(pid) from ".DB::table("forum_post")."");//获取主题数$threads = DB::result_first("select count(tid) from ".DB::table("forum_thread")."");//获取精华主题数$digest = DB::result_first("select count(digest) from ".DB::table("forum_thread")." where digest > 0");//获取高亮主题数$highlight = DB::result_first("select count(highlight) from ".DB::table("forum_thread")." where highlight > 0");//获取置顶主题数$displayorder = DB::result_first("select count(displayorder) from ".DB::table("forum_thread")." where displayorder > 0");//获取附件主题数$attachment = DB::result_first("select count(tid) from ".DB::table("forum_thread")." where attachment > 0");//获取待审核主题数$moderate_threads = DB::result_first("select count(id) from ".DB::table("forum_thread_moderate")." where status = 0");//获取已忽略主题数$moderate_ignore_threads = DB::result_first("select count(id) from ".DB::table("forum_thread_moderate")." where status = 1");//获取待审核回复数$moderate_posts = DB::result_first("select count(id) from ".DB::table("forum_post_moderate")." where status = 0");//获取已忽略回复数$moderate_ignore_posts = DB::result_first("select count(id) from ".DB::table("forum_post_moderate")." where status = 1");//获取主题回收站数$recyclebin_threads = DB::result_first("select count(tid) from ".DB::table("forum_thread")." where displayorder = -1");//获取回复回收站数$recyclebin_posts = DB::result_first("select count(tid) from ".DB::table("forum_post")." where invisible = -5");//获取访问总数$views = DB::result_first("select sum(views) from ".DB::table("forum_thread")."");//获取回复总数$replies = DB::result_first("select sum(replies) from ".DB::table("forum_thread")."");//获取支持总数$recommend_add = DB::result_first("select sum(recommend_add) from ".DB::table("forum_thread")."");//获取反对总数$recommend_sub = DB::result_first("select sum(recommend_sub) from ".DB::table("forum_thread")."");//获取收藏总数$favtimes = DB::result_first("select sum(favtimes) from ".DB::table("forum_thread")."");//获取分享总数$sharetimes = DB::result_first("select sum(sharetimes) from ".DB::table("forum_thread")."");//获取会员:男性$users_man = DB::result_first("select count(uid) from ".DB::table("common_member_profile")." where gender = 1 ");//获取会员:女性$users_woman = DB::result_first("select count(uid) from ".DB::table("common_member_profile")." where gender = 2 ");//获取会员:80后$users_80 = DB::result_first("select count(uid) from ".DB::table("common_member_profile")." where birthyear > 1980 and birthyear < 1989 ");
复制代码
我要说一句 收起回复
一花一世界,一叶一追寻。一曲一场叹,一生为一人。

评论1

Discuz智能体Lv.8 发表于 4 天前 | 查看全部
在Discuz! X中,数据查询是非常常见的操作,尤其是在开发插件或进行二次开发时。你提供的代码片段展示了如何通过SQL查询来获取Discuz! X中的各种数据。以下是对这些查询的详细解释和一些优化建议:

### 1. 调用最新主题
  1. $new = DB::fetch_all("SELECT tid, subject FROM ".DB::table('forum_thread')." ORDER BY `dateline` DESC LIMIT 0,1;");
复制代码

- **解释**: 这个查询从`forum_thread`表中获取最新的主题(`tid`和`subject`),并按`dateline`字段降序排列,限制结果为1条。
- **优化建议**: 如果你只需要一条记录,可以使用`DB::fetch_first`代替`DB::fetch_all`,这样可以减少内存占用。

### 2. 调用最新主题封面图
  1. $img = DB::fetch_all("SELECT a.subject, a.tid, b.attachment FROM ".DB::table("forum_thread")." a LEFT JOIN ".DB::table("forum_threadimage")." b ON b.tid = a.tid WHERE a.`attachment` = 2 ORDER BY a.`dateline` DESC LIMIT 0,5");
复制代码

- **解释**: 这个查询从`forum_thread`表和`forum_threadimage`表中联合查询,获取带有封面图的最新主题,并按`dateline`降序排列,限制结果为5条。
- **优化建议**: 如果封面图不是必须的,可以考虑去掉`LEFT JOIN`,或者确保`forum_threadimage`表中的数据量不会太大,以免影响查询性能。

### 3. 调用当前登录用户阅读权限
  1. $read = DB::result(DB::query("SELECT b.readaccess FROM ".DB::table('common_member')." a LEFT JOIN ".DB::table('common_usergroup_field')." b ON b.groupid = a.groupid WHERE a.`uid` = '$_G'"));
复制代码

- **解释**: 这个查询从`common_member`表和`common_usergroup_field`表中联合查询,获取当前登录用户的阅读权限。
- **优化建议**: 确保`$_G`变量已经正确初始化,并且包含当前用户的UID。如果`$_G`变量未定义或为空,可能会导致SQL错误。

### 4. 调用常用统计数据
  1. // 获取VERSION、RELEASE
  2. $version = unserialize($_G);

  3. // 获取站长用户名
  4. $name = DB::result_first("SELECT username FROM ".DB::table("common_member")." WHERE uid = 1");

  5. // 获取建站时间
  6. $time = DB::result_first("SELECT regdate FROM ".DB::table("common_member")." WHERE uid = 1");

  7. // 获取模板个数
  8. $templates = DB::result_first("SELECT COUNT(styleid) FROM ".DB::table("common_style")."");

  9. // 获取插件个数
  10. $plugins = DB::result_first("SELECT COUNT(pluginid) FROM ".DB::table("common_plugin")."");

  11. // 获取会员人数
  12. $users = DB::result_first("SELECT COUNT(uid) FROM ".DB::table("common_member")."");

  13. // 获取临时会员人数
  14. $guests = DB::result_first("SELECT COUNT(conopenid) FROM ".DB::table("common_connect_guest")."");

  15. // 获取会员总人数
  16. $all_users = $users + $guests;

  17. // 获取管理团队人数
  18. $admins = DB::result_first("SELECT COUNT(uid) FROM ".DB::table("common_member")." WHERE groupid IN (1,2)");

  19. // 获取最新注册会员
  20. $newuser = DB::result_first("SELECT username FROM ".DB::table("common_member")." ORDER BY regdate DESC");

  21. // 获取发帖会员人数
  22. $post_posts = DB::result_first("SELECT COUNT(uid) FROM ".DB::table("common_member_count")." WHERE posts > 0 ");

  23. // 获取发表主题会员人数
  24. $post_threads = DB::result_first("SELECT COUNT(uid) FROM ".DB::table("common_member_count")." WHERE threads > 0 ");

  25. // 获取版块数
  26. $forums = DB::result_first("SELECT COUNT(fid) FROM ".DB::table("forum_forum")." WHERE type = 'forum' AND status IN (0,1)");

  27. // 获取总帖数
  28. $posts = DB::result_first("SELECT COUNT(pid) FROM ".DB::table("forum_post")."");

  29. // 获取主题数
  30. $threads = DB::result_first("SELECT COUNT(tid) FROM ".DB::table("forum_thread")."");

  31. // 获取精华主题数
  32. $digest = DB::result_first("SELECT COUNT(digest) FROM ".DB::table("forum_thread")." WHERE digest > 0");

  33. // 获取高亮主题数
  34. $highlight = DB::result_first("SELECT COUNT(highlight) FROM ".DB::table("forum_thread")." WHERE highlight > 0");

  35. // 获取置顶主题数
  36. $displayorder = DB::result_first("SELECT COUNT(displayorder) FROM ".DB::table("forum_thread")." WHERE displayorder > 0");

  37. // 获取附件主题数
  38. $attachment = DB::result_first("SELECT COUNT(tid) FROM ".DB::table("forum_thread")." WHERE attachment > 0");

  39. // 获取待审核主题数
  40. $moderate_threads = DB::result_first("SELECT COUNT(id) FROM ".DB::table("forum_thread_moderate")." WHERE status = 0");

  41. // 获取已忽略主题数
  42. $moderate_ignore_threads = DB::result_first("SELECT COUNT(id) FROM ".DB::table("forum_thread_moderate")." WHERE status = 1");

  43. // 获取待审核回复数
  44. $moderate_posts = DB::result_first("SELECT COUNT(id) FROM ".DB::table("forum_post_moderate")." WHERE status = 0");

  45. // 获取已忽略回复数
  46. $moderate_ignore_posts = DB::result_first("SELECT COUNT(id) FROM ".DB::table("forum_post_moderate")." WHERE status = 1");

  47. // 获取主题回收站数
  48. $recyclebin_threads = DB::result_first("SELECT COUNT(tid) FROM ".DB::table("forum_thread")." WHERE displayorder = -1");

  49. // 获取回复回收站数
  50. $recyclebin_posts = DB::result_first("SELECT COUNT(tid) FROM ".DB::table("forum_post")." WHERE invisible = -5");

  51. // 获取访问总数
  52. $views = DB::result_first("SELECT SUM(views) FROM ".DB::table("forum_thread")."");

  53. // 获取回复总数
  54. $replies = DB::result_first("SELECT SUM(replies) FROM ".DB::table("forum_thread")."");

  55. // 获取支持总数
  56. $recommend_add = DB::result_first("SELECT SUM(recommend_add) FROM ".DB::table("forum_thread")."");

  57. // 获取反对总数
  58. $recommend_sub = DB::result_first("SELECT SUM(recommend_sub) FROM ".DB::table("forum_thread")."");

  59. // 获取收藏总数
  60. $favtimes = DB::result_first("SELECT SUM(favtimes) FROM ".DB::table("forum_thread")."");

  61. // 获取分享总数
  62. $sharetimes = DB::result_first("SELECT SUM(sharetimes) FROM ".DB::table("forum_thread")."");

  63. // 获取会员:男性
  64. $users_man = DB::result_first("SELECT COUNT(uid) FROM ".DB::table("common_member_profile")." WHERE gender = 1 ");

  65. // 获取会员:女性
  66. $users_woman = DB::result_first("SELECT COUNT(uid) FROM ".DB::table("common_member_profile")." WHERE gender = 2 ");

  67. // 获取会员:80后
  68. $users_80 = DB::result_first("SELECT COUNT(uid) FROM ".DB::table("common_member_profile")." WHERE birthyear > 1980 AND birthyear < 1989 ");
复制代码

- **解释**: 这些查询用于获取Discuz! X中的各种统计数据,如会员人数、主题数、回复数等。
- **优化建议**: 如果这些查询在同一个页面中频繁使用,可以考虑将这些查询结果缓存起来,以减少数据库的查询压力。可以使用Discuz!自带的缓存机制,或者使用外部缓存系统如Redis。

### 总结
- **性能优化**: 对于频繁使用的查询,建议使用缓存机制来减少数据库的负载。
- **代码优化**: 尽量使用`DB::fetch_first`代替`DB::fetch_all`,以减少内存占用。
- **安全性**: 确保所有用户输入的数据都经过适当的过滤和转义,以防止SQL注入攻击。

如果你有更多关于Discuz! X开发的问题,欢迎继续提问!
-- 本回答由 人工智能 AI智能体 生成,内容仅供参考,请仔细甄别。
我要说一句 收起回复

回复

 懒得打字嘛,点击右侧快捷回复【查看最新发布】   【应用商城享更多资源】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

AI智能体
投诉/建议联系

discuzaddons@vip.qq.com

未经授权禁止转载,复制和建立镜像,
如有违反,按照公告处理!!!
  • 联系QQ客服
  • 添加微信客服

联系DZ插件网微信客服|最近更新|Archiver|手机版|小黑屋|DZ插件网! ( 鄂ICP备20010621号-1 )|网站地图 知道创宇云防御

您的IP:3.141.46.77,GMT+8, 2025-3-26 08:17 , Processed in 0.264432 second(s), 73 queries , Gzip On, Redis On.

Powered by Discuz! X5.0 Licensed

© 2001-2025 Discuz! Team.

关灯 在本版发帖
扫一扫添加微信客服
QQ客服返回顶部
快速回复 返回顶部 返回列表