-1

Im having some troubles showing the unique values from my database. As an example, I have this very simple test table:

enter image description here

Where you can see that there is a duplicated value on the Tags column, named intro.

I would like to echo all of the UNIQUE tags, and I've tried with the DISTINCT command, but I might be doing something wrong.

This is my actual query:

SELECT DISTINCT tags FROM blog

But this gives me ALL of the tags.
Any help would be appreciated, thanks.

Gekyzo
  • 41
  • 5
  • 8
    Fix your data structure so you are not storing tags in a comma-delimited list. Instead, use a table with one row per `blogId` and `tag`. – Gordon Linoff Dec 13 '16 at 12:29
  • ^ I was just going to say something to that effect. – Funk Forty Niner Dec 13 '16 at 12:30
  • Agree, also - I am assuming this is `mysql`, if that is the case, could you tag the question with the proper DBMS? It may aid in finding a solution to your current problem... aside from redesigning your `blog` table. – gmiley Dec 13 '16 at 12:31
  • and those spaces in there count for something also. You're best to start over following @GordonLinoff 's suggestion. – Funk Forty Niner Dec 13 '16 at 12:31
  • Also, there is a fairly helpful question/answer out here already. See: http://stackoverflow.com/questions/19073500/sql-split-comma-separated-row discussing the `SUBSTRING_INDEX()` function and splitting your comma separated list into individual rows within a query. – gmiley Dec 13 '16 at 12:35
  • You don't have duplicates. "intro" is not the same as "intro , php , warning , errors" – Disillusioned Dec 13 '16 at 12:40
  • I think you should first separate the comma separated values and then find the unique the values. – Rohit Gupta Dec 13 '16 at 13:01
  • About all those separations between my tags (intro , whatever , ) I just wrote that to test a different approach to the DISTINCT command. I mean, I've also tried to do the query without the spaces wich I'll remove right now. Thanks for the advice. – Gekyzo Dec 13 '16 at 14:45

1 Answers1

0

According to the image there are just to rows in the table, one contains "intro" and the other contains "intro, php, warning, errors". If so, there are not the same.

I think you try to use a row for each value. To do it you should insert each value in a separated sentence, or in the same but using different rows, something like:

insert into blog(tags) values ("intro"),values ("intro"),values ("php")

David Marciel
  • 865
  • 1
  • 12
  • 29