简体   繁体   English

在一栏中订购group_concat值

[英]order group_concat values in one column

I have a database with 4 tables related to this question. 我有一个与此问题相关的4个表的数据库。 I have only provided the required columns for each table to not get things complicated. 我只为每个表提供了必需的列,以免使事情复杂化。

  • reviews 评论

    ID | ID | reviewID reviewID


  • themes 主题

    ID | ID | themeID | themeID | name 名称


  • reviewthemes 评论主题

    ID | ID | reviewID | reviewID | themeID themeID


  • reviewsubthemes 评论主题

    ID | ID | reviewID | reviewID | themeID themeID


So a review can have multiple themes and multiple sub-themes. 因此,评论可以具有多个主题和多个子主题。 Each sub-theme should correspond to a theme, meaning a sub-theme cannot exist without its main theme. 每个子主题都应与一个主题相对应,这意味着没有其主主题就不能存在一个子主题。

The themes table is the place which holds the name for either the theme or sub-theme. themes表是存放主题或子主题名称的地方。

At the minute i have made a query which grabs all the data i require. 在这一刻,我进行了查询,可以获取我需要的所有数据。 Because it makes multiple rows i have done a group_concat for the themes and sub-themes. 因为它使多行,所以我为主题和子主题做了一个group_concat。 This means for each individual review i have a column called "themes". 这意味着对于每个个人评论,我都有一个称为“主题”的列。 In this column it contains all the themes and sub-themes separated by a comma. 在此列中,包含用逗号分隔的所有主题和子主题。

This is my query: 这是我的查询:

select `reviews`.`reviewID`,
   GROUP_CONCAT(`themes`.`name`) as `Name`
from `reviews`

left join `reviewthemes` on `reviewthemes`.`reviewId` =`reviews`.`reviewId`
left join `reviewsubthemes` on `reviewsubthemes`.`reviewid` = `reviews`.`reviewid`
left join `themes` on (`themes`.`themeid` = `reviewthemes`.`themeid` or `themes`.`themeid` = `reviewsubthemes`.`themeid`)

where `reviews`.`healthwatchID` = 32 
and (`review_created` BETWEEN '2015-08-01 00:00:00' AND '2015-08-31 23:59:59')
group by `reviews`.`reviewID`

This brings back data which looks like this: 这将带回看起来像这样的数据:

在此处输入图片说明

Now here is the tricky part, and i'm not sure if i will need to do this in PHP or MYSQl but its safe to say i'm am really confused. 现在这是棘手的部分,我不确定是否需要在PHP或MYSQl中执行此操作,但是可以肯定地说我真的很困惑。

I require the format to be like : 我要求格式如下:

theme - subtheme; theme - subtheme; theme;

So you can see i need a theme and a subtheme to have a semi colon at the end. 因此,您可以看到我需要一个主题和一个子主题才能在末尾使用半冒号。 But some reviews can have a theme with no sub-theme followed by a theme with a sub-theme. 但是有些评论的主题可以不包含子主题,其次可以是带有子主题的主题。

What is the best way to go about this? 最好的方法是什么?

You can use CONCAT to join the name and sub name together, and then GROUP_CONCAT on the results of that:- 您可以使用CONCAT将名称和子名称连接在一起,然后对结果进行GROUP_CONCAT:

select reviews.reviewID,
   GROUP_CONCAT(CONCAT_WS(' - ', t1.name, t2.name) SEPARATOR ';') as Name
from reviews

left join reviewthemes on reviewthemes.reviewId =reviews.reviewId
left join reviewsubthemes on reviewsubthemes.reviewid = reviews.reviewid
left join themes t1 on (t1.themeid = reviewthemes.themeid)
left join themes t2 on (t2.themeid = reviewsubthemes.themeid)
where reviews.healthwatchID = 32 
and (review_created BETWEEN '2015-08-01 00:00:00' AND '2015-08-31 23:59:59')
group by reviews.reviewID

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

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