可以使用ThinkPHP5的模型关联查询来实现该需求。

首先,在模型中定义文章表和评论表的关联关系:

// Article模型 public function comments() { return $this->hasMany('Comment', 'aid'); }

// Comment模型 public function article() { return $this->belongsTo('Article', 'aid'); }

然后,在控制器中编写查询逻辑:

// 获取评论最多的文章 $articles = Article::withCount('comments')->order('comments_count', 'desc')->limit(10)->select();

// 渲染视图 return $this->fetch('index', ['articles' => $articles]);

最后,在视图中生成文章列表:

php 现在有两张表分别是文章表、评论表其中评论表使用aid关联了文章表的id我想获得评论最多的文章并生成列表。用thinkphp5的写法

原文地址: https://www.cveoy.top/t/topic/bZsi 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录