Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7720017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:33:32+00:00 2026-06-01T03:33:32+00:00

I am writing a web app in PHP using mySQL that models an election.

  • 0

I am writing a web app in PHP using mySQL that models an election.

I have three tables: Candidates, Elections, and Votes. Votes contains CandidateID, ElectionID and Count, which is the number of times that the given candidate was voted for in the given Election. Votes also contains TimeStamp which is the last time the row was modified which is used for breaking ties (the earlier vote wins). A candidate may have run in multiple elections. How do I find how many elections a given candidate has ever won?

All help greatly appreciated, thanks.

Some sample data:

CREATE TABLE IF NOT EXISTS `Votes` (
`ElectionID` int(11) unsigned NOT NULL,
`CandidateID` int(11) unsigned NOT NULL,
`Count` smallint(5) unsigned NOT NULL DEFAULT '0',
`stamp` int(11) unsigned NOT NULL,
PRIMARY KEY (`ElectionID`,`CandidateID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

INSERT INTO `Votes` (`ElectionID`, `CandidateID`, `Count`, `stamp`) 
VALUES
(1, 1, 3, 1332897534),
(4, 1, 3, 1333149930),
(4, 4, 2, 1333149947),
(4, 5, 3, 1333149947),
(1, 4, 4, 1333153373);

Desired output: One row, with one column, being the number of wins for a certain candidate

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T03:33:34+00:00Added an answer on June 1, 2026 at 3:33 am

    You can write:

    SELECT COUNT(1)
      FROM Elections AS e
     INNER
      JOIN Votes AS v1 -- representing the candidate of interest
        ON v1.ElectionID = e.ID
       AND v1.CandidateID = ...
      LEFT
     OUTER
      JOIN Votes AS v2 -- representing a candidate who beat the candidate of interest
        ON v2.ElectionID = e.ID
       AND (    v2.Count > v1.Count
             OR (     v2.Count = v1.Count
                  AND v2.stamp < v1.stamp
                )
           )
     WHERE v2.ElectionID IS NULL -- meaning that no candidate beat the candidate of interest
    ;
    

    (It’s also possible to represent either or both of those joins with EXISTS and a correlated subquery; or the first join could be changed to IN with an uncorrelated subquery; but the above is the most likely to perform best, IMHO, and my experience on StackOverflow has been that people seem to like joins better than subqueries for some reason. If you’d prefer a subquery answer, let me know.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a simple web app in PHP that needs to have write access
I'm writing a web app in PHP (using Codeigniter). I'm just wanting some security
I'm writing a web app using php. The main file (index.php) has some Hebrew
I am writing a little web app for ocean tides using PHP. I am
I'm writing php web-app using unholy alliance of php+oracle+iis :) every time script being
We're writing a web app that relies on Javascript/jQuery. It involves users filling out
A web app I am writing in JavaScript using node.js. I use Foreman, but
I'm writing a web app that serves H.264 encoded MP4 video. In Chrome and
I'm writing a web app that at one point allows a user to upload
At work we have a web app that we'll need to interface with another

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.