Raphael Schubert Raphael Schubert - 6 months ago 8
MySQL Question

Can't Count results from Joined Table mySQL

i followed 3 others SO answers, and they say it works.. but here not... here is my problem, i have this QUERY:

SELECT
q.*
FROM relacionamento AS r
INNER JOIN questoes AS q ON r.idquestao = q.id
WHERE tabela = 'disciplina'
GROUP BY q.id


This is there result of query:
Query Result

Until here, it was perfect.. now i need know how much rows returned... i changed the query to this:

SELECT
COUNT(q.*)
FROM relacionamento AS r
INNER JOIN questoes AS q ON r.idquestao = q.id
WHERE tabela = 'disciplina'
GROUP BY q.id


And got this error:

Error when counting

Why i cant count how much rows did i get?

UPDATE:
I did try do this query also:

SELECT
COUNT(*)
FROM relacionamento AS r
INNER JOIN questoes AS q ON r.idquestao = q.id
WHERE tabela = 'disciplina'
GROUP BY q.id


And get this as result:

Count(*)Result

Answer

You can change it like this:

SELECT COUNT(*) FROM (
    SELECT
        q.*
    FROM relacionamento AS r
    INNER JOIN questoes AS q ON r.idquestao = q.id
    WHERE tabela = 'disciplina'
    GROUP BY q.id
)