简体   繁体   English

mySQL GROUP_CONCAT加入

[英]mySQL GROUP_CONCAT IN JOIN

I have the following SQL. 我有以下SQL。 I am trying to perform a GROUP_CONCAT within a Join but no matter how I seem try it doesnt seem to work right. 我正在尝试在Join中执行GROUP_CONCAT,但是无论我如何尝试,它似乎都无法正常工作。

Essentially there is a link tag table that does a one to many on another table that holds tags. 本质上,存在一个链接标签表,该表在另一个包含标签的表上一对多地进行操作。 So there might be say 8 tags for one article record. 因此,一篇文章记录可能有8个标签。 I need them concatenated together .vs. 我需要将它们串联在一起。 ending up with 8 records. 最终得到8条记录。

SELECT 
  `articles`.`art_title`,
  `articles`.`art_bodytext`,
  `info`.`inf_start_date`,
  `info`.`inf_end_date`,
  `info`.`inf_hits`,
  `info`.`inf_acl`,
  `category`.`name`,
  `options`.`opt_tpl`,
  `options`.`opt_published`,
  `options`.`opt_options`,
  `options`.`opt_acl`,
  `tags`.`tag`
FROM
  `linktable`
  INNER JOIN `articles` ON (`linktable`.`lnk_data_id` = `articles`.`art_id`)
  INNER JOIN `info` ON (`articles`.`art_info_id` = `info`.`inf_id`)
  INNER JOIN `category` ON (`articles`.`art_cat_id` = `category`.`id`)
  AND (`articles`.`art_cat_tree_id` = `category`.`fid`)
  INNER JOIN `options` ON (`articles`.`art_opt_id` = `options`.`opt_id`)
  INNER JOIN `linktags` ON (`articles`.`art_tag_set_id` = `linktags`.`lnk_tagset_id`)
  LEFT OUTER JOIN GROUP_CONCAT(`tags`) ON (`linktags`.`lnk_tag_id` = `tags`.`tag_id`)
WHERE
  `linktable`.`lnk_pgc_id` = 1
  • Move the GROUP_CONCAT() into the field list, it is not a join condition, but a field calculation. GROUP_CONCAT()移到字段列表中,这不是联接条件,而是字段计算。
  • Add a GROUP BY to find the rows to combine 添加GROUP BY以查找要合并的行

This gives 这给

SELECT 
  `articles`.`art_title`,
  `articles`.`art_bodytext`,
  `info`.`inf_start_date`,
  `info`.`inf_end_date`,
  `info`.`inf_hits`,
  `info`.`inf_acl`,
  `category`.`name`,
  `options`.`opt_tpl`,
  `options`.`opt_published`,
  `options`.`opt_options`,
  `options`.`opt_acl`,
  GROUP_CONCAT(`tags`.`tag`) AS tags
FROM
  `linktable`
  INNER JOIN `articles` ON (`linktable`.`lnk_data_id` = `articles`.`art_id`)
  INNER JOIN `info` ON (`articles`.`art_info_id` = `info`.`inf_id`)
  INNER JOIN `category` ON (`articles`.`art_cat_id` = `category`.`id`)
  AND (`articles`.`art_cat_tree_id` = `category`.`fid`)
  INNER JOIN `options` ON (`articles`.`art_opt_id` = `options`.`opt_id`)
  INNER JOIN `linktags` ON (`articles`.`art_tag_set_id` = `linktags`.`lnk_tagset_id`)
  LEFT JOIN `tags` ON (`linktags`.`lnk_tag_id` = `tags`.`tag_id`)
WHERE
  `linktable`.`lnk_pgc_id` = 1
GROUP BY `articles`.`art_id`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM