I have 2 table posts and translation
table post
- post_id
- post_content
translation
- post_id
- language
- content
This database will serve many language.
What is the best query to get a post with all available language in a row.
input
post_id = 2
output
->single row with this fields
post_id = 2
post_content = 'lorem ipsum'
post_content_en = 'english lorem ipsum'
post_content_jp = 'japanese lorem ipsum'
post_content_...= 'bla..bla..bla..' //the post_content_xx is language international code
I have try using group_concat
SELECT a.post_id,GROUP_CONCAT(b.language,'--lang_content_separator--',b.content SEPARATOR '---main_separator---') AS all_languages
FROM posts a
LEFT JOIN translation b ON a.post_id = b.post_id
WHERE a.post_id = 2;
The above query is working, but I don’t like it, because we have to set group_concat_max_len.
Is there any trick, or alternate query to replace that group_concat?
I’m sure my solution can be improved, but an alternative is to do the merging of the arrays inside of PHP. In terms of the SQL query, you can run the query. The join in your query seems to be unnecessary.
From PHP, you can then retrieve and format the data using the following code.