当 LEFT JOINing 两个不同的表时,GROUP_CONCAT 联系太多重复值
GROUP CONCAT concats too much duplicate values when LEFT JOINing two different tables
我有一个查询获取 posts 我离开加入两个 tables:
类别和标签: LEFT JOIN 到链接 table →∀INNER JOIN 到类别和标签名称 table.
LEFT JOIN wp_term_relationships AS term_link ON wpp.ID = term_link.object_id
INNER JOIN wp_terms AS terms ON term_link.term_taxonomy_id = terms.term_id
额外的 post 细节作为 post 元:
LEFT JOIN wp_postmeta AS postmeta ON wpp.ID = postmeta.post_id
现在。我想用逗号显示列表中的所有类别,使用 MySQL GROUP_CONCAT:
GROUP_CONCAT(terms.name SEPARATOR ', ') AS allcatname
但是,问题是,由于我的第 2 个 LEFT JOIN 到 wp_postmeta,以某种方式为要找到的每个 wp_postmeta 记录复制了所有类别。所以我的 'allcatname' 看起来像这样:
drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drink
所以发生了...
我想保留 GROUP_CONCAT 的原因是因为我需要它通过 "HAVING" 进行过滤。 所以我正在寻找一种方法,使每个类别记录仅显示一个类别,该类别记录链接到每个 post: 让组 concat 显示。例如。 "drinks, soda, cola" 如果它有这三个类别。
这是完整的查询:
SELECT
SQL_CALC_FOUND_ROWS
wpp.ID, wpp.post_title, wpp.post_author,
wpp.post_status,
GROUP_CONCAT(terms.name SEPARATOR ', ') AS allcatname
FROM wp_posts AS wpp
LEFT JOIN wp_term_relationships AS term_link ON wpp.ID = term_link.object_id
INNER JOIN wp_terms AS terms ON term_link.term_taxonomy_id = terms.term_id
LEFT JOIN wp_postmeta AS postmeta ON wpp.ID = postmeta.post_id
WHERE wpp.post_type = 'post'
AND wpp.post_warning <> 'no_image'
AND wpp.post_status <> 'trash'
AND wpp.post_status <> 'auto-draft'
AND (post_title LIKE '%$search_string%' OR postmeta.meta_value LIKE '%$search_string%')
GROUP BY wpp.ID
ORDER BY post_date DESC
LIMIT 20
使用:
GROUP_CONCAT(DISTINCT terms.name SEPARATOR ', ') AS allcatname
而不是:
GROUP_CONCAT(terms.name SEPARATOR ', ') AS allcatname
我有一个查询获取 posts 我离开加入两个 tables:
类别和标签: LEFT JOIN 到链接 table →∀INNER JOIN 到类别和标签名称 table.
LEFT JOIN wp_term_relationships AS term_link ON wpp.ID = term_link.object_id
INNER JOIN wp_terms AS terms ON term_link.term_taxonomy_id = terms.term_id
额外的 post 细节作为 post 元:
LEFT JOIN wp_postmeta AS postmeta ON wpp.ID = postmeta.post_id
现在。我想用逗号显示列表中的所有类别,使用 MySQL GROUP_CONCAT:
GROUP_CONCAT(terms.name SEPARATOR ', ') AS allcatname
但是,问题是,由于我的第 2 个 LEFT JOIN 到 wp_postmeta,以某种方式为要找到的每个 wp_postmeta 记录复制了所有类别。所以我的 'allcatname' 看起来像这样:
drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drinks, drink
所以发生了...
我想保留 GROUP_CONCAT 的原因是因为我需要它通过 "HAVING" 进行过滤。 所以我正在寻找一种方法,使每个类别记录仅显示一个类别,该类别记录链接到每个 post: 让组 concat 显示。例如。 "drinks, soda, cola" 如果它有这三个类别。
这是完整的查询:
SELECT
SQL_CALC_FOUND_ROWS
wpp.ID, wpp.post_title, wpp.post_author,
wpp.post_status,
GROUP_CONCAT(terms.name SEPARATOR ', ') AS allcatname
FROM wp_posts AS wpp
LEFT JOIN wp_term_relationships AS term_link ON wpp.ID = term_link.object_id
INNER JOIN wp_terms AS terms ON term_link.term_taxonomy_id = terms.term_id
LEFT JOIN wp_postmeta AS postmeta ON wpp.ID = postmeta.post_id
WHERE wpp.post_type = 'post'
AND wpp.post_warning <> 'no_image'
AND wpp.post_status <> 'trash'
AND wpp.post_status <> 'auto-draft'
AND (post_title LIKE '%$search_string%' OR postmeta.meta_value LIKE '%$search_string%')
GROUP BY wpp.ID
ORDER BY post_date DESC
LIMIT 20
使用:
GROUP_CONCAT(DISTINCT terms.name SEPARATOR ', ') AS allcatname
而不是:
GROUP_CONCAT(terms.name SEPARATOR ', ') AS allcatname