Getting commer seperated data from mySQL

Discussion in 'PHP' started by cyclotron, Nov 21, 2010.

  1. #1
    Hey there,

    I have a table, over 20,000 entries. Each entry has a column called 'tags'

    Each tag field has something like 'blue, flashing, animated' etc etc

    I want to somehow pull the data out without the spaces, or commas, and if some of the tags are used more than once, not show them twice.

    So for example:

    Entry 1:
    Tags: blue, flashing, animated

    Entry 2:
    Tags: red, flashing, animated

    I want to echo:


    blue, red, flashing, animated


    Could anyone guide me? Thanks!
     
    cyclotron, Nov 21, 2010 IP
  2. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #2
    For php you can use
    array_unique()
    PHP:
    or by mysql you could add a 'GROUP by' statement.

    If you show us the code your working with presently, someone can advise a solution.
     
    MyVodaFone, Nov 21, 2010 IP
  3. jagat21

    jagat21 Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi,

    You can use following mysql functions for generating comma seperated data :

    #

    CONCAT() returns NULL if any argument is NULL.

    mysql> SELECT CONCAT('My', 'S', 'QL');
    -> 'MySQL'
    mysql> SELECT CONCAT('My', NULL, 'QL');
    -> NULL
    mysql> SELECT CONCAT(14.3);
    -> '14.3'

    For quoted strings, concatenation can be performed by placing the strings next to each other:

    mysql> SELECT 'My' 'S' 'QL';
    -> 'MySQL'

    #

    CONCAT_WS(separator,str1,str2,...)

    CONCAT_WS() stands for Concatenate With Separator and is a special form of CONCAT(). The first argument is the separator for the rest of the arguments. The separator is added between the strings to be concatenated. The separator can be a string, as can the rest of the arguments. If the separator is NULL, the result is NULL.

    mysql> SELECT CONCAT_WS(',','First name','Second name','Last Name');
    -> 'First name,Second name,Last Name'
    mysql> SELECT CONCAT_WS(',','First name',NULL,'Last Name');
    -> 'First name,Last Name'
     
    jagat21, Nov 24, 2010 IP