Home > Back-end >  How to combine two column of text using python sql
How to combine two column of text using python sql

Time:10-01

id    fk_case_id      content
1        468          This is to inform the record
2        468          tommorrow for studies
3        469          Hello welcome to school

from connect_database import mycursor,db
mycursor.execute(f"select content,pk_content_id,fk_case_id from mt_page_file_contents")
myresult = mycursor.fetchall()
for x in myresult:
    text = x[0]

I need to join column row 1 and 2 based on unique fk_case_id and get solution "This is to inform the record tommorrow for studies". Is there any solution

CodePudding user response:

You can try this.

Postgres:

SELECT fk_case_id, string_agg(content, ' ') as contents
FROM mt_page_file_contents
GROUP BY fk_case_id;

MySQL:

SELECT fk_case_id, GROUP_CONCAT(content, " ") as contents
FROM mt_page_file_contents
Group by fk_case_id;

    

This will give you concatenated content by grouping on the basis of fk_case_id.

fk_case_id      contents
   468          This is to inform the record tommorrow for studies
   469          Hello welcome to school
  • Related