Możesz to zrobić za pomocą substring_index() . Poniższe zapytanie używa Twojego zapytania jako podzapytania, a następnie stosuje tę logikę:
select Name, ISOCode_2,
substring_index(currencies, ',', 1) as Currency1,
(case when numc >= 2 then substring_index(substring_index(currencies, ',', 2), ',', -1) end) as Currency2,
(case when numc >= 3 then substring_index(substring_index(currencies, ',', 3), ',', -1) end) as Currency3,
(case when numc >= 4 then substring_index(substring_index(currencies, ',', 4), ',', -1) end) as Currency4,
(case when numc >= 5 then substring_index(substring_index(currencies, ',', 5), ',', -1) end) as Currency5,
(case when numc >= 6 then substring_index(substring_index(currencies, ',', 6), ',', -1) end) as Currency6,
(case when numc >= 7 then substring_index(substring_index(currencies, ',', 7), ',', -1) end) as Currency7,
(case when numc >= 8 then substring_index(substring_index(currencies, ',', 8), ',', -1) end) as Currency8
from (SELECT country.Name, country.ISOCode_2, group_concat(currency.name) AS currencies,
count(*) as numc
FROM country
INNER JOIN countryCurrency ON country.country_id = countryCurrency.country_id
INNER JOIN currency ON currency.currency_id = countryCurrency.currency_id
GROUP BY country.name
) t
Wyrażenie substring_index(currencies, ',' 2) przenosi listę w walutach do drugiej. W przypadku Somoa amerykańskiego będzie to 'US Dollar,Kwanza' . Następne wywołanie z -1 jako argument przyjmuje ostatni element listy, którym będzie 'Kwanza' , który jest drugim elementem currencies .
Należy również zauważyć, że zapytania SQL zwracają dobrze zdefiniowany zestaw kolumn. Zapytanie nie może mieć zmiennej liczby kolumn (chyba że używasz dynamicznego SQL poprzez prepare oświadczenie).