标签(空格分隔): mysql concat concat_ws
1.concat(字段1,字段2,......) 函数的作用:将多个字符串连成一个字符串
select concat(name,age) from user where id=10;//张三18
#####2.concat_ws(连接符,字段1,字段2,......)作用:将多个字符串用连接符连接成一个字符串
select concat_ws('_',name,age,email);//张三_18_xxx@163.com
concat_ws()字段有null的元素会被直接过滤
select concat_ws('_',name,age,email);
//张三_xxx@163.com age值为null会被直接过滤
concat()和concat_ws()函数连接字段不限制个数 但超过100以上会有性能影响
示例:厨品秀多店同名物料分组sql
Expense::leftJoin('expense_material as em','em.expenseId','=','expenses.id')
->leftJoin('material as m','em.materialId','=','m.id')
->leftJoin('categories as c','c.id','=','m.firstCategory')
->whereIn('expenses.shopId',$shopList)
->select([\DB::raw("concat_ws('_',c.name,m.name,m.specification) as keyName"),'c.name as categoryName','em.excludeTaxTotal as amount','em.materialId','m.name as materialName','m.specification'])
->get()->toArray();