I’m currently working on a project of Voting System. I have a mysql database named “dbName” and
fields
`for table "pospresident"
---------------|-------------|---------------- \n
idCandidate | NumOfVote | CandidateName \n
---------------|-------------|---------------- \n
1 | 20 | Joe
2 | 30 | Carlo
---------------+-------------+----------------
for table "posvpres"
---------------|-------------|----------------
idCandidate | NumOfVote | CandidateName
---------------|-------------|----------------
1 | 15 | Anne
2 | 35 | Lucas
---------------+-------------+----------------
and so on ...
`
I just want to display all the names of candidate in different table who get the highest number of votes. I just need an idea on how my SELECT query should be.
Use
UNION ALLto get the data from the two tables, then useGROUP BYwithSUMandHAVINGto get only this candidate that has theMAXvotes. Something like this:SQL Fiddle Demo
This will give you: