mysql 合并列 函数 GROUP_CONCAT

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);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值