Reputation: 3
I have two table in database Table1: candidates
id | name
--------
1 | John
2 | Eva
3 | Siera
Table2: votes
| candidateid |
--------
| 1 |
| 1 |
| 1 |
| 1 |
| 2 |
| 2 |
| 3 |
Please someone help me my question is about to how can i select the name of person from candidates who have more entries in votes table.?
Upvotes: 0
Views: 744
Reputation: 28834
COUNT
function, over a grouping of candidate id.LIMIT 1
to get the details of the person with highest votes.Try the following:
SELECT c.id,
c.name,
COUNT(*) AS total_votes
FROM candidates AS c
JOIN votes AS v ON v.candidateid = c.id
GROUP BY c.id
ORDER BY total_votes DESC LIMIT 1
Upvotes: 1
Reputation: 1269563
There is no voteId
in your sample data. You seem to want:
SELECT CandidateId, count(*)
FROM votes
GROUP by CandidateId;
You can use a JOIN
to bring in the name from the other table.
Upvotes: 0