0
Simply put, I have two tables, perguntas
and respostas
, for each pergunta
4
or more respostas
, turns out I’m not getting numerar
as linhas
correctly when grouped by questions.
My result:
| linha | pergunta | resposta |
|-------|-------------------|----------|
| 1 | Qual o seu nome? | gino |
| 5 | Qual a sua idade? | 20 |
Intended:
| linha | pergunta | resposta |
|-------|-------------------|----------|
| 1 | Qual o seu nome? | gino |
| 2 | Qual a sua idade? | 20 |
Schema:
CREATE TABLE `perguntas` (
`id` int(10) UNSIGNED NOT NULL,
`pergunta` varchar(50) COLLATE utf8_unicode_ci NOT NULL
);
INSERT INTO `perguntas` (`id`, `pergunta`) VALUES
(1,'Qual o seu nome?'),
(2,'Qual a sua idade?');
CREATE TABLE `respostas` (
`id` int(11) NOT NULL,
`pergunta_id` int(11) NOT NULL,
`resposta` varchar(50) COLLATE utf8_unicode_ci NOT NULL
);
INSERT INTO `respostas` (`id`,`pergunta_id`,`resposta`) VALUES
(1,1,'gino'),
(2,1,'lato'),
(2,1,'sapo'),
(2,1,'dode'),
(1,2,'20'),
(2,2,'30'),
(2,2,'40'),
(2,2,'50');
Consultation:
Select @contador := @contador + 1 AS linha,
pergunta, resposta from (SELECT @contador := 0) AS nada,
perguntas p
inner join respostas r on r.pergunta_id=p.id
group by p.id
There’s no logic to it
from (SELECT @contador := 0)
– rbz
Because there’s no logic?
– Miguel Silva
I got this code right here
– Miguel Silva
In fact this select makes a roll, using the counter as table, and making
group by p.id
.– rbz
@Rbz
(SELECT @contador := 0)
serves to start the counter with zero when you can’t make aSET @contador := 0;
separate. Normally theSET
becomes more legible and simple to maintain.– Bacco