想在table1表中,查公司中的员工名字有哪些,table1表中员工有重名的情况,所以要对员工名去重,并且要得到员工名字、及其对应的部门号dept_id
即:对name字段去重,并获得name, dept_id 字段
在thinkphp3.2 中
$table1= M('table1');
使用下面方法:
$result = $table1 -> where ( $where )
->field ( 'name,dept_id' )
->distinct(true)
->order('create_time desc') //排序
->select ();
这样使用的话,是对name, dept_id 去重,当dept_id 不同时,即时name 相同也会被查询出来。解决:
$result = $table1 -> where ( $where )
->field ( 'name,dept_id' )
->group('name')
->order('create_time desc') //排序
->select ();
这样就会去重,默认会取出来每个分组的第一行数据,这样就达到了对name去重的目的。
在mysql中:
select * from table group by name
因为select后的字段必须在group by 中出现,暂时先做到查询出全部信息
如果SQL语句还有limit,order by等条件,必须放在group by后面。
这样就达到了既去重,又能够输出更多字段信息的目的.