存档

文章标签 ‘mysql’

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

2008年6月25日 Killman 没有评论

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;

分类: database 标签: