Ron1959
asked on
MySQL query output columns from rows
I want to query a table called Vote_Record and output row data in columns, as follows:
Sorry for the crazy table formatting -- I couldn't figure out how line up the columns...
Thanks!
Table: Vote_Record
+-------+---------+--------+
| Name | Issue| Vote |
+-------+---------+--------+
| Tom | A | Yes |
| Dick | A | No |
| Harry | A | No |
| Tom | B | Yes |
| Dick | B | Yes |
| Harry | B | No |
| Tom | C | Yes |
| Dick | C | Yes |
| Harry | C | Yes |
+-------+---------+--------+
Query Output
+---------+--------------+--------------+-------------+
| Name | Issue A | Issue B | Issue C |
+---------+--------------+--------------+-------------+
| Tom | Yes | Yes | Yes |
| Harry | No | No | Yes |
+---------+--------------+--------------+-------------+
Sorry for the crazy table formatting -- I couldn't figure out how line up the columns...
Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Wow, that's a very clean solution. It should be easy to manage a large number of issues.
Please watch for my next question, which will have to do with "weighting" the votes and summing them for a total score.
Thanks!
Please watch for my next question, which will have to do with "weighting" the votes and summing them for a total score.
Thanks!
You are welcome - thanks for the points.
Matrix inversion is a standard computer science problem, and there are many ways to do it. This demonstrates the PHP logic.
The essential moving parts look something like this (note the position of rows and columns in these two examples).
The essential moving parts look something like this (note the position of rows and columns in these two examples).
// CREATE THE EMPTY MATRIX OF COLUMNS (NAMES) AND ROWS (QUESTIONS)
$mat = array();
foreach ($cols as $c)
{
foreach ($rows as $r)
{
$mat[$c][$r] = '-';
}
}
// CREATE THE EMPTY MATRIX OF COLUMNS (QUESTIONS) AND ROWS (NAMES)
$mat = array();
foreach ($rows as $r)
{
foreach ($cols as $c)
{
$mat[$r][$c] = '-';
}
}
something like this..
Open in new window