ThinkPHP使用distinct和group by去重复的数据
在一对多关联查询中,遇到重复数据的问题,这不是我们想要的结果,下面就是两种常用的去重方法。
1、distinct方法去重
$data=$test_data->Distinct(true)->field('descriprion')->order('description desc')->select();
2、group方法去重
$items = Suspect::alias('s')
->leftJoin("activity a","a.suspect_id = s.id")
->field("s.*,a.id as activity_id")
->group('id')
->select();
Mysql语句:
SELECT `s`.*,a.id as activity_id FROM `suspect` `s` LEFT JOIN `activity` `a` ON `a`.`suspect_id`=`s`.`id` GROUP BY `id`