You can work the result of your query as a string, in order to enable its manipulation without having to presume its existence in a "hard-coded".
Use the aggregation function GROUP_CONCAT
to "merge" into a row each column of your query, using the separator you prefer (here I am using ;
):
select
GROUP_CONCAT(motivo SEPARATOR ';') as motivos,
GROUP_CONCAT(numero SEPARATOR ';') as numeros
from (
select count(*) as numero, motivo from motivos
group by motivo order by numero desc limit 5
) tmp;
***
| motivos | numeros |
|----------------------------------------------|-----------|
| motivo 1;motivo 2;motivo 3;motivo 4;motivo 5 | 3;3;3;3;1 |
Then, for each column to return in a row, concatenate them again, using a line break character \n
between them. I use the function here CONCAT_WS
by allowing to define the general separator at the beginning, allowing the inclusion of more lines in an elegant way, without having to keep repeating the \n
:
select
CONCAT_WS (
'\n',
GROUP_CONCAT(motivo SEPARATOR ';'),
GROUP_CONCAT(numero SEPARATOR ';')
) as resultado
from (
select count(*) as numero, motivo from motivos
group by motivo order by numero desc limit 5
) tmp;
***
| resultado |
|--------------------------------------------------------|
| motivo 1;motivo 2;motivo 3;motivo 4;motivo 5
3;3;3;3;1 |
Note that the result is just a column of a record, with line breaking between the group of motifs and the group of numbers.
If you want two records, one for each group, I believe that a UNION solves the problem, with a little redundancy. It would be more elegant if Mysql supported Common Table Expressions, but This will only come in version 8, so follow example with UNION:
select
GROUP_CONCAT(motivo SEPARATOR ';') as resultado
from (
select count(*) as numero, motivo from motivos
group by motivo order by numero desc limit 5
) tmp
UNION
select
GROUP_CONCAT(numero SEPARATOR ';') as resultado
from (
select count(*) as numero, motivo from motivos
group by motivo order by numero desc limit 5
) tmp;
***
| resultado |
|----------------------------------------------|
| motivo 1;motivo 2;motivo 3;motivo 4;motivo 5 |
| 3;3;3;3;1 |
Follow SQL Fiddle containing the above examples: http://sqlfiddle.com/#! 9/644b0b/26
That said, only for complementary purposes, the UNION with use of CTE would be like this:
with consulta as (
select
GROUP_CONCAT(motivo SEPARATOR ';') as motivos,
GROUP_CONCAT(numero SEPARATOR ';') as numeros
from (
select count(*) as numero, motivo from motivos
group by motivo order by numero desc limit 5
) tmp
)
select motivos from consulta
union
select numeros from consulta;
You really need it to be in separate lines ? What’s the impact?
– Felipe
Unfortunately yes, I am using this information for programming a chart and it only accepts online input.
– Renata
In SQL Server you can do this using
PIVOT
as subselect, but apparently mysql does not have it, you would need to build something that is reusable not to need to change the query every time a new reason is included.– Diego Rafael Souza