Wrong sequence MYSQL line numbering

Asked

Viewed 463 times

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)

  • Because there’s no logic?

  • I got this code right here

  • In fact this select makes a roll, using the counter as table, and making group by p.id.

  • @Rbz (SELECT @contador := 0) serves to start the counter with zero when you can’t make a SET @contador := 0; separate. Normally the SET becomes more legible and simple to maintain.

1 answer

2


Try to get subquery:

SET @n = 0;

SELECT @n := @n+1 AS linha, tab.*
FROM (SELECT pergunta, resposta 
FROM perguntas p
INNER JOIN respostas r ON r.pergunta_id=p.id) as tab;

Functioning in the DB Fiddle

  • I get this error: You have an error in your SQL syntax; check the manual that Corresponds to your Mysql server version for the right syntax to use near '* FROM (SELECT question, answer) FROM questions p INNER JOIN answers r' at line 1

  • Altered.......

  • This result I could already, I need to group by question

  • How will you group by question if the answer is different? There are several records. N:N.

  • I would just like to number the questions in this test generator: http://educar.esy.es/gerarProvas/

  • 1

    You would have to generate for 2 parts then. First the question and then the answers. Repetition of answers within the repetition of questions. Either you generate only the questions and use the counter, or it was as I did. Otherwise, there would be no way to separate the answers later. Or bring grouped by GROUP_CONCAT() and make a explode afterward.

  • I already did, I created a view for questions and another for answers with group_concat, but still it is counting the wrong lines.

  • Solved, I was using group_concat for the respostas, in the search itself, it was enough to create a separate view to solve.

  • Too bad they denied my question

  • 1

    Positive! Solved! rs

  • Vlw same guy!

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.