SQL QUERY - 合并具有相同ID的行中的数据

时间:2012-06-21 06:15:40

标签: sql wordpress

我在我的网站上使用wordpress,我有大量的数据,我想通过SQL导出。 问题是,我在每个帖子(项目)收到几行,类别列下有数据。我想要做的是合并所有类别,所以最终每个帖子只有1行,包含所有类别。

这是我目前的查询:

    SELECT post_title, b.object_id, b.term_taxonomy_id, c.term_id, d.name
FROM wp_posts a
JOIN wp_term_relationships b ON a.ID = b.object_id
JOIN wp_term_taxonomy c ON b.term_taxonomy_id = c.term_taxonomy_id
JOIN wp_terms d ON d.term_id = c.term_id

这是我的结果示例:

post_title     object_id  term_taxonomy_id  term_id name
Holcroft Covenant   2       5                 5         Action
Holcroft Covenant   2       6                 6         Drama
Holcroft Covenant   2       8                 8         Thriller

我想要的结果是: Holcroft Covenant 2 5 5动作,戏剧,惊悚

任何想法?

1 个答案:

答案 0 :(得分:0)

如果您正在使用MySql,请尝试:

SELECT 
    post_title, b.object_id, b.term_taxonomy_id, c.term_id, GROUP_CONCAT(d.name)
FROM wp_posts a
    JOIN wp_term_relationships b ON a.ID = b.object_id
    JOIN wp_term_taxonomy c ON b.term_taxonomy_id = c.term_taxonomy_id
    JOIN wp_terms d ON d.term_id = c.term_id
GROUP BY post_title, b.object_id, b.term_taxonomy_id, c.term_id