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
https://forums.phpfreaks.com/topic/40444-election-style-mysql/
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
https://forums.phpfreaks.com/topic/40444-election-style-mysql/#findComment-196499
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
https://forums.phpfreaks.com/topic/40444-election-style-mysql/#findComment-196558
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.