MySQL查询中的组逗号分隔元素

时间:2021-11-30 00:19:49

It's kinda stupid question and I already made it work using php,but I was wondering can I group all the unique elements in a column separated by comma within my query call.

这是一个愚蠢的问题,我已经使用PHP工作了,但我想知道我可以在查询调用中将逗号分隔的列中的所有唯一元素分组。

I have a column with comma separated tags

我有一个逗号分隔标签的列

  • tag1, tag2
  • tag2, tag3
  • tag3, tag1

currently I am making my query with GROUP BY ('tag_col') which returns as it is (like the list up there). In the end I have to output a loop with all the unique tags, which I've done within my php script. The question had to begin that by using group by it groups as separate rows that contain for example:

目前我正在使用GROUP BY('tag_col')进行查询,它按原样返回(就像那里的列表一样)。最后,我必须输出一个包含所有唯一标记的循环,我已经在我的PHP脚本中完成了。问题必须从使用group by group group作为单独的行开始,例如:

  • tag1, tag2

    and

  • tag1,tag2和

  • tag2, tag1

Ain't there smarter way to list them all as tag1,tag2 etc. before sorting it with php.

在使用php排序之前,是不是有更智能的方法将它们全部列为tag1,tag2等。

1 个解决方案

#1


1  

You can use GROUP_CONCAT for this i guess, it will allow you to concatenate all of them in one row by using GROUP_CONCAT(tag_col) and removing your group by.

我猜你可以使用GROUP_CONCAT,它允许你使用GROUP_CONCAT(tag_col)将所有这些连接在一行并删除你的组。

Then with php you can turn it into an array and from there it's easy to remove any duplicates.

然后使用php,你可以把它变成一个数组,从那里很容易删除任何重复。

#1


1  

You can use GROUP_CONCAT for this i guess, it will allow you to concatenate all of them in one row by using GROUP_CONCAT(tag_col) and removing your group by.

我猜你可以使用GROUP_CONCAT,它允许你使用GROUP_CONCAT(tag_col)将所有这些连接在一行并删除你的组。

Then with php you can turn it into an array and from there it's easy to remove any duplicates.

然后使用php,你可以把它变成一个数组,从那里很容易删除任何重复。