0

I need to get unique values from a table. I have a single column with comma separated keywords. I need to derive a single list of all the keywords without duplicates. Getting the count of how often each keyword is present, too.

From what I have researched, it is an UNPIVOTING like function with an unknown number of columns?

For example:

keywords


  • red, blue, yellow
  • blue, orange, black, white
  • brown, black, clear, pink
  • blue, violet, orange

Result

color | count


  • red 1
  • blue 3
  • yellow 1
  • orange 2
  • black 2
  • white 1
  • brown 1
  • clear 1
  • pink 1
  • violet 1

Thank you in advance!!

** Thus far I have tried adding an explode_table type procedure, but realized I can't call that dynamically from a View. Then I have been experimenting with performing a reverse GROUP_CONCAT() on the column. I haven't been able to produce code that performs.


My version of echo_Me's answer:

    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
1
  • post the query that you tried Commented Jun 26, 2014 at 16:44

1 Answer 1

1

Try that:

    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

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for the answer. A bit over my head, but I'm trying it out. I inserted my column name and try running it in Navicat. I get a 'View's SELECT contains a subquery in the FROM clause' error. I'll post my version of your statement in my question above.
whats wrong ? it works good in my demo , you must have done something wrong.
The code works great in the DEMO. And I can force phpMyAdmin and Navicat to run the statement. But if I try to save the statement into a view, it returns that error. argh.
I just read about mySQL not allowing subqueries. Which explains the error "View's SELECT contains a subquery in the FROM clause". I wonder if it is possible to create a view from the subquery and then call that view in the original query?
you have other solution , is to reorganize your table and put the values separatly with the count. colors and counts . then you dont need subquery , you just need simple query. you have bad structure in your table. dont store values comma separated .
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.