1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

  2. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[SQL] loop on a group concat on MYSQL

Discussão em 'Outras Linguagens' iniciado por Stack, Setembro 4, 2021.

  1. Stack

    Stack Membro Participativo

    I am trying to see if this is possible.

    I have three columns (NAME, TYPE, RATING).

    I am trying to see if I can do a group_concat and layout the cells such as this.

    TABLE EXAMPLE

    | NAME | TYPE | RATING | user_id
    ------------- ------ ---------- ---------
    Hello World1 | Text1 | Star1 | 1
    Hello World1 | Text1 | Star1 | 1
    Hello World2 | Text2 | Star2 | 2
    Hello World2 | Text2 | Star2 | 2
    Hello World2 | Text2 | Star2 | 2
    Hello World3 | Text3 | Star3 | 3
    Hello World3 | Text3 | Star3 | 3
    Hello World4 | Text4 | Star4 | 4
    Hello World4 | Text4 | Star4 | 4


    I want to layout the results like this, where is all grouped in one row, group the user_id

    EXAMPLE A ONE ROW

    NAME: Hello World1, TYPE: Text1, RATING: Star1,
    NAME: Hello World1, TYPE: Text1, RATING: Star1,
    NAME: Hello World2, TYPE: Text2, RATING: Star2,
    NAME: Hello World2, TYPE: Text2, RATING: Star2,
    NAME: Hello World2, TYPE: Text2, RATING: Star2,
    NAME: Hello World3, TYPE: Text3, RATING: Star3,
    NAME: Hello World3, TYPE: Text3, RATING: Star3,
    NAME: Hello World4, TYPE: Text4, RATING: Star4,
    NAME: Hello World4, TYPE: Text4, RATING: Star4


    I know if I do this:

    concat('NAME: ',group_concat(name separator ' : '),
    ' TYPE: ',group_concat(type separator ' : '),
    ' RATING: ',group_concat(rating separator ' : ')
    ),


    I'll get this.

    EXAMPLE B

    NAME: Hello World, Hello World, Hello World, Hello World, Hello World
    TYPE: Text, Text,Text,Text,Text,
    RATING: Star,Star,Star,Star,Star,


    Any ideas on how I can accomplish EXAMPLE A? There are also user_ids that are the same so I am trying to group them all into one row for an import.

    Continue reading...

Compartilhe esta Página