0

我在表中有以下数据results

+-----+-----+-----+------+-----+-----+
| 一个 | 乙| C | D | E | F |
+-----+-----+-----+------+-----+-----+
| 100 | 140 | 120 | 130 | 160 | 150 |
| 200 | 243 | 第374章 435 | 第674章 987 |
| 312 | 324 | 123 | 第456章 378 | 第645章
+-----+-----+-----+------+-----+-----+

什么样的SELECT语句可以让我从结果表中获得最高的 5 个值?

4

1 回答 1

1

什么样的SELECT语句可以让我从结果表中获得最高的 5 个值?

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

但是,就目前情况而言,您可以使用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上查看。

于 2013-07-11T17:11:09.537 回答