Database (My SQL), SQL commands


Basically, through which command, I could calculate the party that gets the most votes, I know that we have to use the Count and MAX command, but when using it at the same time it gives me an error. I leave the command you make and I get an error. Thanks in advance !!!!!

select partidos.nombre,
  from electores, partidos, partidos_electores
 where (electores.id_elector=partidos_electores.id_elector
   and partidos.codigo_partido=partidos_electores.codigo_partido)
 group by partidos.nombre

Appointment in block

asked by Joaquin 24.11.2017 в 21:57

1 answer


Actually, for what you're looking for, you do not need the electores table. With partidos and partidos_electores you have all the necessary information.

You can make a join between the 2 tables and group by matches. Then, instead of using a MAX , I find it easier to simply sort the results by the number of records (votes) and limit the result to the first record:

select p.nombre
  from partidos p
  left join partidos_electores pe
    on pe.codigo_partido = p.codigo_partido
 group by p.codigo_partido, p.nombre
 order by count(pe.codigo_partido) desc
 limit 1
answered by 24.11.2017 в 23:31