1.场景:主表一条数据,字表多条数据。需要查询时候展示一条,字表的数据用逗号分隔
sql语句:
SELECT cdu.user_name as user_name, cmp.user_id as create_uid, cmr.receive_user_id as receive_user_id, cmp.id as id, cmp.mesage_title as mesage_title, cmr.status as status, cmp.create_time as create_time, cmp.message_content as message_content, cmp.status as message_status, IFNULL(GROUP_CONCAT(DISTINCT cmd.doc_name),'') as docName FROM car_message_post cmp LEFT JOIN car_message_receiver cmr ON cmp.id = cmr.message_id INNER JOIN car_adm_user cdu ON cmp.user_id = cdu.user_id LEFT JOIN car_message_doc cmd ON cmd.message_id = cmp.id WHERE 1=1 and cmp.id = 252 GROUP BY cmp.id,mesage_title,cmp.create_time,message_content LIMIT 50;
GROUP_CONCAT(DISTINCT cmd.doc_name) 需要加上distinct,要不然字表出现重复数据