I have a column that look like the following
id | Text
1 | English text1
2 | English text2
3 | English text3
4 | English text4
5 | French text1
6 | French text2
7 | French text3
8 | French text4
9 | Arabic text1
10 | Arabic text2
11 | Arabic text3
12 | Arabic text4
etc…
I want to make like this
id | EngText FreText ArbText
1 | English text1 French text1 Arabic text1
2 | English text2 French text2 Arabic text2
3 | English text3 French text3 Arabic text3
4 | English text4 French text4 Arabic text4
Using phpMyAdmin if possible or MySQL query. By either detecting the text language or the number of id where each 4 rows will be in different column.
I used 4 just as an example but they are actually more than that where each language has the same number of rows.
Thank you
4
Answers
Realize that if you do it this way, you will have to change the database structure if you add or remove a language. This is not good practice.
I understand the urge to do it this way but it is not good practice. Unless you are really very very sure never to have to add or remove a language (are you ever in this world?), go for a table structure where you have a table with an unique id (autonumber), textId, languageId (textId+languageId together unique) and the text in that language. That is basic structural database design.
If you are convinced and want to do it that way, you just need to copy the original table into the new one, with
Next you make a language table to define the languages based on the numbers you get in the result, with columns id, language (VARCHAR)
(Note: I couldn’t post this as a remark as it was too long. Posted a solution that answers the question in the way that the OP wanted (even though bad general practice) as well.)
You can do it as follows :
id/4
will result a number from 0 to 1 for english, 1 to 2 for french and 2 to 3 from arabic, so we can group data by each language usingmax() case when
clause.id mod 4
will help to group data side by side .An other way to do it :
Demo here
I strongly advise against your current approach but this query will provide the result you asked for:
or using the pivot approach:
For both of the above queries, if you have more languages, just change the divisor in the subquery from 3 to whatever your language count is.
What you describe in your example is a relationship between ids (1, 5, 9), (2, 6, 10), (3, 7, 11) & (4, 8, 12) but with no representation of this relationship in your table structure.
A better approach would be to change the structure of your table to include a unique identifier for the string to be translated and an identifier for the language. One way to do this would to treat your English text as the base language string with a structure like:
With the above example, you could make the first four rows reference themselves and add a unique key on (base_lang_id, lang_code).
Another approach is to have a unique text string to identify each string to be translated:
If you want to go your way, since it is a one off action, you go best by creating a new table with all the language fields you need, and just do a query one at a time per language, since your language field have unique names.
And run this for every language with the appropriate field name.