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.

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

  1. mysql select后面能跟多少个表_mysql查询语句中,FROM后面可以跟具体的表名,也可以跟一个查询结果...

    /* FROM后面可以跟具体的表名,也可以跟一个查 询结果(此查询结果其实就是相当于一张表, 注意:必须要给它取一个别名) */ #给查询结果取别名 SELECT * FROM book, (SELE ...

  2. MYSQL优化派生表(子查询)在From语句中的

    Mysql 在5.6.3中,优化器更有效率地处理派生表(在from语句中的子查询): 优化器推迟物化子查询在from语句中的子查询,知道子查询的内容在查询正真执行需要时,才开始物化.这一举措提高了性能 ...

  3. oracle动态语句打开游标,Oracle动态语句中返回游标

    本来以为动态语句只能返回类似int.varchar2这种类型,今天测试了下,发现还支持游标,现测试如下: 创建返回游标的函数: create or replace function testf ret ...

  4. 谷歌浏览器网页表格复制一列_如何冻结或隐藏Google表格中的列和行

    谷歌浏览器网页表格复制一列 The greater the number of rows and columns in your Google Sheets spreadsheet, the more ...

  5. mysql数据库查询关键字级别_MySQL数据库之单表查询中关键字的执行顺序

    MySQL数据库之单表查询中关键字的执行顺序 1 语法顺序 select distinct from where group by having order by limit 2 执行顺序 from ...

  6. python交换两列的位置_如何更改 pandas dataframe 中两列的位置

    如何更改 pandas dataframe 中两列的位置: 把其中的某列移到第一列的位置. 原来的 df 是: df = pd.read_csv('I:/Papers/consumer/codeand ...

  7. wpf的listview编辑指定列_将项目添加到WPF ListView中的列

    小编典典 更少的XAML和更多的C#解决方案 如果您ListView在XAML中定义: 然后,您可以添加列并在C#中填充它: public Window() { // Initialize this. ...

  8. matlab输出多个参数到表格固定列,matlab将excle的列提取出来?如何使用Matlab提取Excel中指定列的数据?...

    MATLAB 在txt文件中怎么提取数据,并将那三列数据转化为EXCLE格式? 在matlab中 file import data number of text header lines 改成6 下一 ...

  9. python 二维数组元素返回二维坐标_python – 从二维数组中返回生成器而不是位置列表...

    我昨天正在进行一场游戏,我必须遍历一个二维阵列并找到标记为"d"的任何单元格的位置(其中单元格表示为" – "表示空白,"d"表示为脏). ...

最新文章

  1. mysql binlog恢复错误_mysql通过binlog日志来恢复数据
  2. 微服务发现与注册之Eureka源码分析
  3. Remote Direct Memory Access (RDMA)
  4. python学习:re模块
  5. installshield 脚本 在卸载过程执行_Linux下运行Jmeter脚本
  6. Nginx 的配置文件介绍
  7. 力扣 数组的相对排序
  8. UNITY读取图片素材的URL并使用
  9. 华为android强刷系统下载地址,华为Y511官方rom固件刷机包下载_Y511原版系统强刷升级包...
  10. 基于Java的旅游门票管理系统JAVA MYSQL
  11. 创业者两大特征:喜欢折腾与坚持不懈
  12. 如何随心意改变桌面快捷方式的图标
  13. 物流快递商家寄件运力查询接口API代码实例
  14. 拼多多校招----大整数相乘(python)
  15. 批量手机号归属地查询
  16. 模态弹窗与非模态弹窗
  17. 统计学学习日记:L6-离散趋势分析之总体方差和标准差
  18. 论文笔记之---Person Re-identification in the Wild
  19. 用Python写安卓APP,你怕不怕
  20. 58同城将在美国纽交所挂牌上市

热门文章

  1. NLP自然语言常见问题及相关模型训练数据格式示例
  2. Python使用tsne进行高维数据可视化实战:二维可视化、三维可视化
  3. R语言绘制箱图(Base R and ggplot2)
  4. python实现二叉树遍历(前序遍历、中序遍历、后序遍历)
  5. 特征重要性、特征集成+FeatureUnion、特征选择变换器+ColumnTransformer、标签特征变换+TransformedTargetRegressor、特征质量、自动学习数据中的特征
  6. SVM+二分类+多分类
  7. 两步聚类算法+Two Step
  8. html5与原生混合模式开发,HTML5与混合模式开发与Native的关系及其实现[张振华.Jack].pdf...
  9. 4 通讯_鼎信通讯:2019年净利润同比下降20.58% 拟10转4派1.04元
  10. 二十七、连续分配管理方式