我需要从表中获取唯一值。我有一列用逗号分隔的关键字。我需要导出所有关键字的单个列表,而不要重复。也可以获取每个关键字出现频率的计数。

根据我的研究,这是一个UNPIVOTING之类的函数,具有未知的列数?

例如:

关键字




红色,蓝色,黄色
蓝色,橙色,黑色,白色
棕色,黑色,透明,粉红色
蓝色,紫色,橙色


结果

颜色|计数




红色1
蓝色3
黄色1
橙色2
黑色2
白色1
棕色1
清除1
粉红1
紫1


先感谢您!!

**
到目前为止,我已经尝试添加explode_table类型的过程,但是意识到我无法从View动态调用它。然后,我一直在尝试对列执行反向GROUP_CONCAT()。我无法生成执行的代码。



我的echo_Me版本是:

    SELECT  SUBSTRING_INDEX(SUBSTRING_INDEX(sKeywords, ',', n.n), ',', -1) value , count(*) as counts
FROM tblPatternMetadata t CROSS JOIN
    (SELECT a.N + b.N * 10 + 1 n FROM (SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL  SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) a, (SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION  ALL SELECT 9) b
    ORDER BY n ) n
    WHERE n.n <= 1 + (LENGTH(sKeywords) - LENGTH(REPLACE(sKeywords, ',', ''))) group by value

最佳答案

试试看:

    SELECT  SUBSTRING_INDEX(SUBSTRING_INDEX(t.keywords, ',', n.n), ',', -1) value , count(*) as counts
    FROM table1 t CROSS JOIN
   (
   SELECT a.N + b.N * 10 + 1 n
   FROM
    (SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL  SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) a
   ,(SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION  ALL SELECT 9) b

   ORDER BY n
 ) n
 WHERE n.n <= 1 + (LENGTH(t.keywords) - LENGTH(REPLACE(t.keywords, ',', '')))
 group by value


DEMO HERE

关于mysql - MySQL唯一值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24435902/

10-11 02:52