0

I need to select all values from all rows by one column in my table. I have

ID       TAGS
1        girls, womens, cars
2        girls, world, flowers
3        weapons, cars, boys

so after SQL I should get an array:

array('girls', 'womens', 'cars', 'girls', 'world', 'flowers', 'weapons', 'cars', 'boys')

so how my sql should look like?

3 Answers3

5

Never, never, never store multiple values in one column.

Like you see now this will only give you headaches. Normalize your table.

USER_ID  TAG
1        girls
1        women
1        cars
2        girls
...

After that you could select your desired result like this

select group_concat(distinct tag)
from your_table
Community
  • 1
  • 1
juergen d
  • 186,950
  • 30
  • 261
  • 325
0

Use GROUP_CONCAT(). A similar answer is already here MySQL DISTINCT on a GROUP_CONCAT().

Community
  • 1
  • 1
Wasif
  • 512
  • 2
  • 6
0

I can atleast give a sample solution:

  1. use Cursor or something to traverse through each row of data
  2. split the data (there are couple of split example found in internet) sample example, Split string in SQL
  3. put the value in a temp table
  4. finally select distinct from the table

someone can put these example and make a solution.

Community
  • 1
  • 1
saif
  • 114
  • 1
  • 1
  • 10