Home > $Programming > 有关 sum与limit组合使用时的问题

有关 sum与limit组合使用时的问题

select sum(a) from test_table order by b desc limit 1,2

这个句子,在MySQL 和 Sqlite3下,如此 sum得到的是 Empty Set

而 select sum(a) from test_table order by b desc limit 0,2 则返回全部记录的a字段的合计,limit不起作用。

但是去掉sum函数的话, select a from test_table order by b desc limit 1,2 能够选择出需要的行。

为什么? 来自 MySQL 官方站点论坛的解释:

The LIMIT clause affects only the number of rows returned, and this query in fact only returns one row. You might want to try something like this:

SELECT sum(quantity)
FROM (SELECT quantity
FROM stock_card
LIMIT 0,2
) AS subquery;

Related posts:

  1. [译] Orx教程:0.Basic
  2. Vim 快捷键
  3. 算法解题思考过程[总结]
  4. Impdp/Expdp 之并行(Parallel)和压缩(Compression)
  5. DBCP连接池(一):原理与基本配置
Categories: $Programming Tags:
  1. No comments yet.
  1. No trackbacks yet.