GROUP_CONCAT()是MySQL数据库提供的一个函数,通常跟GROUP BY一起用,具体可参考MySQL官方文挡:http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat。
语法:
语法:
GROUP_CONCAT([DISTINCT] expr [,expr ...] [ORDER BY {unsigned_integer | col_name | expr} [ASC | DESC] [,col_name ...]] [SEPARATOR str_val]) 1.例如:SELECT student_id, GROUP_CONCAT(courses_id) AS courses FROM student_courses WHERE student_id=2 GROUP BY student_id;
public Page<Enclosure> find(Page<Enclosure> page,String enclosurename,String mobilenum,String name) {
StringBuffer sqlBuffer = new StringBuffer();
sqlBuffer.append("select * from (");
sqlBuffer.append("select GROUP_CONCAT(DISTINCT info.mobilenum) mobilenum,GROUP_CONCAT(DISTINCT info.name) name,GROUP_CONCAT(DISTINCT info.id) infoids,IFNULL(a.id,0) id,a.enclosurename enclosurename,IFNULL(a.staffid,0) staffid,a.latitude,a.longitude,a.datetime from cg_enclosure a LEFT JOIN cg_enclosure_mobile b on a.id=b.enclosureId");
sqlBuffer.append(" LEFT JOIN cg_mobile_information info on b.mobileinfoid=info.id");
sqlBuffer.append(" GROUP BY a.enclosurename");
sqlBuffer.append(") cg where cg.id!=0 ");
// 围栏名称
if (enclosurename!= null&&!"".equals(enclosurename)) {
sqlBuffer.append(" and cg.enclosurename like '%" + enclosurename+ "%' ");
}
// 手机号
if ( mobilenum!= null&&!"".equals(mobilenum)) {
sqlBuffer.append(" and cg.mobilenum like '%" + mobilenum+ "%' ");
}
//姓名
if(name!= null&&!"".equals(name)){
sqlBuffer.append(" and cg.name like '%" + name+ "%' ");
}
sqlBuffer.append("order by cg.id");
String sql = sqlBuffer.toString();
// System.out.println("================"+sql+"=============");
return enclosureDao.findBySql(page,sql,Enclosure.class);