此函数的功能是拼接一个字段的多条记录
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])
例如:
#查询预约信息
SELECT
app.appointment_id,
app.username,
app.appointment_time,
apt.appointment_type,
GROUP_CONCAT(DISTINCT apt.`name`) AS NAME,
app.appointment_department,
app.other_name,
app.appointment_target,
app.appointment_point,
app.appointment_person
FROM
crm_appointment app,
crm_apt_relation apt
WHERE app.appointment_id=apt.appointment_id
GROUP BY app.appointment_id;
结果: