How to select the most used foreign key?

0

I would like to make a SELECT where you would select the foreign key ( idTema ) most used

Example:

That example above would be 1

How would that SELECT ?

    
asked by anonymous 14.06.2018 / 22:20

2 answers

2

I do not know if it's the best way to build the select but I think it works.

SELECT idTema FROM nome.tabela GROUP BY idTema ORDER BY count(idTema) DESC LIMIT 1;
    
14.06.2018 / 22:31
2

You have to group by idTema , sort by quantity in descending order and limit by 1 record:

SELECT a.'idTema', COUNT(*) qtde
FROM nome_da_tabela a
GROUP BY a.'idTema'
ORDER BY qtde DESC
LIMIT 1;

See more about GROUP BY here .

    
14.06.2018 / 22:23