A表是用户表,B表是文章表,按本周发布文章数量倒序获取A表数据。框架是thinkphp.
$subsql = BModel::alias('b')
->whereTime('createtime', 'w')
->field('b.user_id,count(b.id) count1')
->group('b.user_id')
->buildSql();
$lists = AModel::alias('p')
->join([$subsql => 'b'], 'p.id=b.user_id', 'left')
->field('p.id,p.name,IFNULL(b.count1,0) as count')
->order('count desc')
->paginate();
这样就实现了获取A表数据,按B表关联计数进行排序。