热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

Imaginetwoquestionsfromanonlinesurvey:Doyoulikeapples?Resultstoredinmysqldbcolumnq1as1fo

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.



推荐阅读
  • MySQL索引详解与优化
    本文深入探讨了MySQL中的索引机制,包括索引的基本概念、优势与劣势、分类及其实现原理,并详细介绍了索引的使用场景和优化技巧。通过具体示例,帮助读者更好地理解和应用索引以提升数据库性能。 ... [详细]
  • 本文介绍如何使用Objective-C结合dispatch库进行并发编程,以提高素数计数任务的效率。通过对比纯C代码与引入并发机制后的代码,展示dispatch库的强大功能。 ... [详细]
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • C++实现经典排序算法
    本文详细介绍了七种经典的排序算法及其性能分析。每种算法的平均、最坏和最好情况的时间复杂度、辅助空间需求以及稳定性都被列出,帮助读者全面了解这些排序方法的特点。 ... [详细]
  • 扫描线三巨头 hdu1928hdu 1255  hdu 1542 [POJ 1151]
    学习链接:http:blog.csdn.netlwt36articledetails48908031学习扫描线主要学习的是一种扫描的思想,后期可以求解很 ... [详细]
  • 本文探讨了如何在给定整数N的情况下,找到两个不同的整数a和b,使得它们的和最大,并且满足特定的数学条件。 ... [详细]
  • 本文详细介绍了Java中org.w3c.dom.Text类的splitText()方法,通过多个代码示例展示了其实际应用。该方法用于将文本节点在指定位置拆分为两个节点,并保持在文档树中。 ... [详细]
  • 本文详细介绍了 Apache Jena 库中的 Txn.executeWrite 方法,通过多个实际代码示例展示了其在不同场景下的应用,帮助开发者更好地理解和使用该方法。 ... [详细]
  • 本文介绍如何通过创建替代插入触发器,使对视图的插入操作能够正确更新相关的基本表。涉及的表包括:飞机(Aircraft)、员工(Employee)和认证(Certification)。 ... [详细]
  • 本实验主要探讨了二叉排序树(BST)的基本操作,包括创建、查找和删除节点。通过具体实例和代码实现,详细介绍了如何使用递归和非递归方法进行关键字查找,并展示了删除特定节点后的树结构变化。 ... [详细]
  • 本文详细介绍了C语言中链表的两种动态创建方法——头插法和尾插法,包括具体的实现代码和运行示例。通过这些内容,读者可以更好地理解和掌握链表的基本操作。 ... [详细]
  • 本文详细探讨了VxWorks操作系统中双向链表和环形缓冲区的实现原理及使用方法,通过具体示例代码加深理解。 ... [详细]
  • 根据最新发布的《互联网人才趋势报告》,尽管大量IT从业者已转向Python开发,但随着人工智能和大数据领域的迅猛发展,仍存在巨大的人才缺口。本文将详细介绍如何使用Python编写一个简单的爬虫程序,并提供完整的代码示例。 ... [详细]
  • 本文详细介绍了 MySQL 中 LAST_INSERT_ID() 函数的使用方法及其工作原理,包括如何获取最后一个插入记录的自增 ID、多行插入时的行为以及在不同客户端环境下的表现。 ... [详细]
  • 本文介绍了如何通过 Maven 依赖引入 SQLiteJDBC 和 HikariCP 包,从而在 Java 应用中高效地连接和操作 SQLite 数据库。文章提供了详细的代码示例,并解释了每个步骤的实现细节。 ... [详细]
author-avatar
无心小象
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有