mysql select 返回列,是否可以对在mysql SELECT语句中返回列的顺序进行排序?

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 << something else >>

+-------+-------+

| q2Sum | q1Sum |

+-------+-------+

| 12 | 10 |

+-------+-------+

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=1 and No=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't really answer the question of 'is it possible to sort returned columns' and I'm not even sure my assumption is correct on the 'right way to do this'...

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 @user2864740 and @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 @q1Sum = (SELECT SUM(q1) FROM myTable);

SET @q2Sum = (SELECT SUM(q2) FROM myTable);

CREATE TEMPORARY TABLE orderedSums (

qNum VARCHAR(5),

qSum INT

);

INSERT INTO orderedSums

VALUES ('q1Sum', @q1Sum),('q2Sum', @q2Sum);

SELECT qNum, qSum

FROM orderedSums

ORDER BY qSum DESC;

Returns multiple rows of SUMs sorted in descending order from myTable.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值