gpt4 book ai didi

mysql查询表中的高值

转载 作者:行者123 更新时间:2023-11-29 13:44:37 27 4
gpt4 key购买 nike

我在结果表中有以下数据:

+-----+-----+-----+-----+-----+-----+|  A  |  B  |  C  |  D  |  E  |  F  |+-----+-----+-----+-----+-----+-----+| 100 | 140 | 120 | 130 | 160 | 150 || 200 | 243 | 374 | 435 | 674 | 987 || 312 | 324 | 123 | 456 | 378 | 645 |+-----+-----+-----+-----+-----+-----+

哪种 SELECT 语句可以从结果表中获取最高的 5 个值?

最佳答案

What sort of SELECT statement can get me the highest 5 values from the results table?

每列的处理方式相同,这表明您的架构是非规范化的。如果数据标准化,则此操作将大大简化,以便合并列(但会创建一个附加列来指示每个记录源自哪一列)。

但是,就目前情况而言,您可以使用 UNION :

  (SELECT A col FROM results ORDER BY col DESC LIMIT 5)
UNION ALL
(SELECT B col FROM results ORDER BY col DESC LIMIT 5)
UNION ALL
(SELECT C col FROM results ORDER BY col DESC LIMIT 5)
UNION ALL
(SELECT D col FROM results ORDER BY col DESC LIMIT 5)
UNION ALL
(SELECT E col FROM results ORDER BY col DESC LIMIT 5)
UNION ALL
(SELECT F col FROM results ORDER BY col DESC LIMIT 5)
ORDER BY col DESC
LIMIT 5

查看 sqlfiddle .

关于mysql查询表中的高值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17599293/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com