Turi
Turi

Reputation: 3

Concatenate different rows into one in mysql

I have this database ("statement") where each word of a sentence is in a different row

SENTENCE |  SEQUENCE |  TEXT
    A   |   1       |   MY  
    A   |   2       |   NAME
    A   |   3       |   IS
    A   |   99      |   CARL
    B   |   1       |   MY  
    B   |   2       |   NAME
    B   |   3       |   IS
    B   |   99      |   JUSTINE

I would like to do a select where the result is the complete sentence, like:

 SENTENCE | TEXT
    A   |   MY  NAME IS CARL
    B   |   MY  NAME IS JUSTINE

since I cannot create a new table, I would like to put the command in the select just to have a new column. In each sentence the words are sorted in the column "sequence" and 99 is always the last word of the sentence.

I've tried adapting this solution but it didn't work.

Thank you very much

Turi

Upvotes: 0

Views: 45

Answers (1)

juergen d
juergen d

Reputation: 204904

select sentence, group_concat(text order by sequence separator ' ')
from your_table
group by sentence

Upvotes: 1

Related Questions