Imagine two questions from an online survey:
Do you like apples?
Result stored in mysql db column "q1" as 1 for Yes or 0 for No
Do you like oranges?
Result stored in mysql db column "q2" as 1 for Yes or 0 for No
Imagine the following SELECT statement:
SELECT SUM(q1) AS q1Sum, SUM(q2) AS q2Sum FROM myTable
Assume q1Sum=10 and q2Sum=12. The current SELECT statement would return:
+-------+-------+
| q1Sum | q2Sum |
+-------+-------+
| 10 | 12 |
+-------+-------+
But is it possible to return a different column order so that the greatest SUM is always the first column returned without changing the list order in the SELECT statement?
SELECT SUM(q1) AS q1Sum, SUM(q2) AS q2Sum FROM myTable <>
&#43;-------&#43;-------&#43;
| q2Sum | q1Sum |
&#43;-------&#43;-------&#43;
| 12 | 10 |
&#43;-------&#43;-------&#43;
If q1Sum becomes greater than q2Sum the column order returned would revert to the original order.
Why am I asking this question?
I have inherited a large table of multiple columns from a survey similar to above with Yes&#61;1 and No&#61;0 responses to multiple questions. I assume the right way to do this is to create variables that hold the SUM values and insert them into temp table rows and then select/sort returned rows. However, this doesn&#39;t really answer the question of &#39;is it possible to sort returned columns&#39; and I&#39;m not even sure my assumption is correct on the &#39;right way to do this&#39;...
While I know how to sort the results once returned regardless of order (using php in my case), I was curious if there was a way to automatically sort the returned columns in mysql so that the highest value is always the first column and the SUM-ed results in the remaining columns decrease numerically for the remaining columns returned.
Curious if there is an elegant answer. While it is easy to sort ROWS returned in mysql using ORDER BY, I have not seen an example of how to sort COLS of a single returned row of multiple SUM-ed values as articulated above. Imagine 10 questions above instead of 2. I am assuming this is not possible but hope someone can prove me wrong...in a nice way.
解决方案
Thanks &#64;user2864740 and &#64;sgeddes for your prompt replies.
SURVEY SAYS: It is NOT possible to sort returned columns in a SELECT statement using mysql without using dynamic sql.
The solution I came up with uses a temporary table and is similar to the example below. While perhaps less than ideal, it answered the mail for what I needed. Curious if there is a better way to do the same thing or if I messed anything else up below.
DROP TEMPORARY TABLE IF EXISTS orderedSums;
SET &#64;q1Sum &#61; (SELECT SUM(q1) FROM myTable);
SET &#64;q2Sum &#61; (SELECT SUM(q2) FROM myTable);
CREATE TEMPORARY TABLE orderedSums (
qNum VARCHAR(5),
qSum INT
);
INSERT INTO orderedSums
VALUES (&#39;q1Sum&#39;, &#64;q1Sum),(&#39;q2Sum&#39;, &#64;q2Sum);
SELECT qNum, qSum
FROM orderedSums
ORDER BY qSum DESC;
Returns multiple rows of SUMs sorted in descending order from myTable.