I have the following table which contains the following data:
id | text | language |
---|---|---|
1 | German text | German |
2 | English text | English |
What I want is to get results in the following format:
german="deutscher text" english="english text"
means not :
text="deutscher text" text="english text"
Key/column nametext
should be data from language
I tried the following query but it doesn't work:
SELECT text as (SELECT language FROM `table` where id = 1) FROM `table` where id = 1;
(SELECT language FROM
table where id = 1)
will return "german", so the query should be:
"Select german text from table
where id = 1;" but this doesn't work.
Is there a way to do this in one query?
Cheers, Thomas
You have to change the table schema slightly; added a reference to group by languages you want to use
Then SQL
Virtual data
One option you can use is
PREPARED STATMENT
:The first step is to dynamically allocate the columns required by the
@sql
variable. The previously assigned@sql
variable is then concatenated with the rest of the finalSELECT
query and reassigned to the@sql
variable. The query will be as follows:Finally, we prepare, execute and then deallocate the statement allocated in the
@sql
variable and you will get the expected results.Demo Violin