简体   繁体   English

MySQL DISTINCT 在 GROUP_CONCAT()

[英]MySQL DISTINCT on a GROUP_CONCAT()

I am doing SELECT GROUP_CONCAT(categories SEPARATOR ' ') FROM table .我正在做SELECT GROUP_CONCAT(categories SEPARATOR ' ') FROM table Sample data below:下面的示例数据:

categories
----------
test1 test2 test3
test4
test1 test3
test1 test3

However, I am getting test1 test2 test3 test4 test1 test3 back and I would like to get test1 test2 test3 test4 back.但是,我得到了test1 test2 test3 test4 test1 test3我想得到test1 test2 test3 test4回来。 Any ideas?有任何想法吗?

Many thanks!非常感谢!

GROUP_CONCAT具有DISTINCT属性:

SELECT GROUP_CONCAT(DISTINCT categories ORDER BY categories ASC SEPARATOR ' ') FROM table

Using DISTINCT will work 使用DISTINCT将有效

SELECT GROUP_CONCAT(DISTINCT(categories) SEPARATOR ' ') FROM table

REf:- this REf:- 这个

Other answers to this question do not return what the OP needs, they will return a string like: 此问题的其他答案不返回OP的需要,它们将返回类似以下的字符串:

test1 test2 test3 test1 test3 test4

(notice that test1 and test3 are duplicated) while the OP wants to return this string: (注意, test1test3是重复的),而OP希望返回此字符串:

test1 test2 test3 test4

the problem here is that the string "test1 test3" is duplicated and is inserted only once, but all of the others are distinct to each other ( "test1 test2 test3" is distinct than "test1 test3" , even if some tests contained in the whole string are duplicated). 这里的问题是,字符串"test1 test3"被复制并插入只有一次,但所有的人都不同的情况下( "test1 test2 test3"比明"test1 test3" ,即使一些测试包含在整个字符串重复)。

What we need to do here is to split each string into different rows, and we first need to create a numbers table: 我们需要在这里将每个字符串分成不同的行,我们首先需要创建一个数字表:

CREATE TABLE numbers (n INT);
INSERT INTO numbers VALUES
(1),(2),(3),(4),(5),(6),(7),(8),(9),(10);

then we can run this query: 然后我们可以运行以下查询:

SELECT
  SUBSTRING_INDEX(
    SUBSTRING_INDEX(tableName.categories, ' ', numbers.n),
    ' ',
    -1) category
FROM
  numbers INNER JOIN tableName
  ON
    LENGTH(tableName.categories)>=
    LENGTH(REPLACE(tableName.categories, ' ', ''))+numbers.n-1;

and we get a result like this: 我们得到这样的结果:

test1
test4
test1
test1
test2
test3
test3
test3

and then we can apply GROUP_CONCAT aggregate function, using DISTINCT clause: 然后我们可以使用DISTINCT子句应用GROUP_CONCAT聚合函数:

SELECT
  GROUP_CONCAT(DISTINCT category ORDER BY category SEPARATOR ' ')
FROM (
  SELECT
    SUBSTRING_INDEX(SUBSTRING_INDEX(tableName.categories, ' ', numbers.n), ' ', -1) category
  FROM
    numbers INNER JOIN tableName
    ON LENGTH(tableName.categories)>=LENGTH(REPLACE(tableName.categories, ' ', ''))+numbers.n-1
  ) s;

Please see fiddle here . 请看这里的小提琴。

SELECT
  GROUP_CONCAT(DISTINCT (category))
FROM (
  SELECT
    SUBSTRING_INDEX(SUBSTRING_INDEX(tableName.categories, ' ', numbers.n), ' ', -1) category
  FROM
    numbers INNER JOIN tableName
    ON LENGTH(tableName.categories)>=LENGTH(REPLACE(tableName.categories, ' ', ''))+numbers.n-1
  ) s;   

This will return distinct values like: test1,test2,test4,test3 这将返回不同的值,例如: test1,test2,test4,test3

DISTINCT :将为您提供独特的价值。

SELECT GROUP_CONCAT(DISTINCT(categories )) AS categories FROM table

You can simply add DISTINCT in front. 您只需在前面添加DISTINCT

SELECT GROUP_CONCAT(DISTINCT categories SEPARATOR ' ')

if you want to sort, 如果你想排序,

SELECT GROUP_CONCAT(DISTINCT categories ORDER BY categories ASC SEPARATOR ' ')

select group_concat(distinct(tablename.column1)) as column1 from tablename select group_concat(distinct(tablename.column1)) as column1 from tablename

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

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