sql查询以获取结果以逗号分隔

时间:2013-05-16 04:51:16

标签: mysql phpmyadmin

我有2个表,想要通过逗号分隔来获取所有类别。

SELECT object_id, meta_value, name, event_level, event_temp_id FROM 
wp_em_meta, wp_terms, wp_em_events WHERE wp_em_meta.meta_key = 'category-image' 
AND wp_em_meta.object_id IN ($eventtid) AND wp_em_meta.object_id = wp_terms.term_id 
AND wp_em_meta.object_id = wp_em_events.event_temp_id

例如:

表1


enter image description here     在附加的图像中,我想以enter image description here

的形式获取结果

1 个答案:

答案 0 :(得分:1)

您可以在MySQL中使用GROUP_CONCAT()功能

SELECT object_id, 
GROUP_CONCAT(event_level) as meta_value, 
name, event_level, event_temp_id FROM 
wp_em_meta, wp_terms, wp_em_events 
WHERE wp_em_meta.meta_key = 'category-image' 
AND wp_em_meta.object_id IN ($eventtid) AND wp_em_meta.object_id = wp_terms.term_id 
AND wp_em_meta.object_id = wp_em_events.event_temp_id
GROUP BY object_id