How can I SELECT a table1 column value with multiple table2 row values as replaced?
So the detailed problem definition is below;
Post Table
ID description
-------------------
1 <a href="http://example.com?attachment_id=10">File1</a>, <a href="http://example.com?attachment_id=11">File2</a>
2 <a href="http://example.com?attachment_id=12">File3</a>, <a href="http://example.com?attachment_id=13">File4</a>
Attachment Table
ID postId guid
10 1 http://example.com/file1.jpg
11 1 http://example.com/file2.jpg
12 2 http://example.com/file3.jpg
13 2 http://example.com/file4.jpg
I want to get the first row (id: 1) in Post Table with the values replaced as corresponding 2 records in Attachment Table (which is id: 10 and id: 11)
Query result should be as below:
Expected Query Result:
ID description
-------------------
1 <a href="http://example.com/file1.jpg">File1</a>, <a href="http://example.com/file2.jpg">File2</a>
2 <a href="http://example.com/file3.jpg">File3</a>, <a href="http://example.com/file4.jpg">File4</a>
I tried following query but only first value in description is replaced.
SELECT
post.ID,
REPLACE(post.description, CONCAT('http://example.com?attachment_id=', attachment.ID), attachment.guid) AS description
FROM post
JOIN attachment ON post.ID = attachment.postId
GROUP BY post.ID
Also, I tried with REGEXP_REPLACE variation but I couldn't achieve the expected result.
Note: This is not the real data model, the tables are given to simplify the problem definition. I am trying to migrate data from an old WordPress system to Drupal 9. So in reality the data is kept in same table (posts and attachments in regards to post_type) and I build query with self join. What I need actually is, the file addresses in content. So I should replace the attachment URLs with file URLs.