您可以使用
GROUP_CONCAT:
SELECt person_id, GROUP_CONCAt(hobbies SEPARATOR ', ')FROM peoples_hobbiesGROUP BY person_id;
DISTINCT运算符以避免重复:
SELECt person_id, GROUP_CONCAt(DISTINCT hobbies SEPARATOR ', ')FROM peoples_hobbies GROUP BY person_id;
正如Jan在他们的评论中所述,您还可以在将值内插之前使用
ORDER BY以下方法对值进行排序:
SELECt person_id, GROUP_CONCAt(hobbies ORDER BY hobbies ASC SEPARATOR ', ')FROM peoples_hobbiesGROUP BY person_id;
正如达格(Dag)在其评论中所述,结果限制为1024个字节。要解决此问题,请在查询之前运行以下查询:
SET group_concat_max_len = 2048;
当然,您可以
2048根据需要进行更改。要计算和分配值:
SET group_concat_max_len = CAST( (SELECt SUM(LENGTH(hobbies)) + COUNT(*) * LENGTH(', ') FROM peoples_hobbies GROUP BY person_id) AS UNSIGNED);
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)