Jump to content

Election-style mySQL?


Wainwright

Recommended Posts

Ahoy there!

 

I've got an odd question. I help run a political simulation, and the software we use to determine election results exports results to .csv, which is perfect. The problem is that I want to do a results display module, and I'm not sure how to make that work.

 

The catch is that it either exports these results as a string ("Gold Party 2506 Green Party 1290 Blue Party 801"), which simply isn't much use to me in any context, or as hard-wired columns, for example:

 

Constituency Electorate Gold Party Blue Party Green Party
St. Mary Mead    21005 7008 12557 210

 

This is fine and well, but I'd like to display results ordered by vote total (I.E: "Blue Party: 12557, Gold Party: 7008, Green Party: 210") instead of in the order of these hard-wired columns. Is there any way I can make this happen?

Link to comment
Share on other sites

Fair enough... take a look at the mockup I've got over here.

 

You may have to visit the Advancer to kick-start it. Instead of displaying results by party, though, what I'd like to do is display results by current vote total. (I.E: If the Labour party has 500 votes and the Conservative party has 201, then Labour should display above the Conservatives in the per-constituency list.) The problem is that the numbers in question are in the database in the fashion I mocked up above... each party has a column for their vote counts.

 

Is there any way to reconcile the two goals?

Link to comment
Share on other sites

Databases can't sort across columns.  Though you can kludge it.

 

The easiest way to do this is to read the data into php and sort it there.

 

If you REALLY want to do it in mysql, you can do something like

 

SELECT Constituency, 'Electorate' as party, Electorate FROM votes

UNION ALL SELECT Constituency, 'Gold Party' as party, `Gold Party` FROM votes

...

 

and so on for each column.  Then you can just sort by votes.  Essentially you need to convert the columns into rows before you can do any kind of sorting in sql.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.