
I am developing a web application for the selection of the BEM chairman with the CodeIgniter framework. This application is almost complete. However, there is one thing that still makes me confused, namely determining the winner after the election is finished.
The first query - in my opinion appropriate - obtained from a search on Google is as follows:
SELECT
calon_idcalon,
calon.nama AS nama,
calon.foto AS foto,
COUNT(calon_idcalon) AS jumlah
FROM
kotak_suara,
calon
WHERE
calon_idcalon = calon.nim
GROUP BY calon_idcalon
ORDER BY jumlah
DESC LIMIT 1
The results obtained from the query are one person with the highest number of votes. But, how about if there are two people who have the most sura.
When using the above query, the results remain one person.
Finally, I was given a powerful query from one of the MySQL Indonesia member groups . This powerful query can display all winners with the same vote.
SELECT
calon_idcalon,
calon.nama AS nama,
calon.foto AS foto,
COUNT(calon_idcalon) AS jumlah
FROM
kotak_suara,
calon
WHERE
calon_idcalon = calon.nim
GROUP BY
calon_idcalon
HAVING
COUNT( calon_idcalon) >= (
SELECT COUNT( calon_idcalon ) AS SUM
FROM kotak_suara
GROUP BY calon_idcalon
ORDER BY SUM
DESC LIMIT 1
)

0 Komentar untuk "MySQL Query to Determine Winners"
Silahkan berkomentar sesuai artikel