mysql最小结果集_mysql-SQL选择最小结果集,其总和覆盖给定值

我需要SQL才能选择总和> = 260的最小的最旧记录集.

我在数据库中定义的数据如下:

CREATE TABLE record (

id bigint(20) NOT NULL AUTO_INCREMENT,

function varchar(10) NOT NULL,

amount decimal(4,2) NOT NULL,

timestamp datetime NOT NULL,

journal int NOT NULL,

PRIMARY KEY (id)

) ENGINE=InnoDB;

insert into record(journal,function, amount, timestamp) values (1, 'debit', 81.15, '2013-01-01 01:01:02');

insert into record(journal,function, amount, timestamp) values (2, 'debit', 23.33, '2013-01-01 01:01:04');

insert into record(journal,function, amount, timestamp) values (1, 'debit', 68.19, '2013-01-01 01:01:06');

insert into record(journal,function, amount, timestamp) values (2, 'debit', 29.93, '2013-01-01 01:01:08');

insert into record(journal,function, amount, timestamp) values (1, 'debit', 71.01, '2013-01-01 01:01:10');

insert into record(journal,function, amount, timestamp) values (2, 'debit', 71.62, '2013-01-01 01:01:12');

insert into record(journal,function, amount, timestamp) values (1, 'debit', 88.94, '2013-01-01 01:01:14');

insert into record(journal,function, amount, timestamp) values (2, 'debit', 82.72, '2013-01-01 01:01:16');

insert into record(journal,function, amount, timestamp) values (1, 'debit', 44.26, '2013-01-01 01:01:18');

insert into record(journal,function, amount, timestamp) values (2, 'debit', 69.04, '2013-01-01 01:01:20');

insert into record(journal,function, amount, timestamp) values (1, 'debit', 96.83, '2013-01-01 01:01:22');

insert into record(journal,function, amount, timestamp) values (1, 'credit', 81.27, '2013-01-01 01:01:01');

insert into record(journal,function, amount, timestamp) values (2, 'credit', 30.86, '2013-01-01 01:01:03');

insert into record(journal,function, amount, timestamp) values (1, 'credit', 95.62, '2013-01-01 01:01:05');

insert into record(journal,function, amount, timestamp) values (2, 'credit', 16.20, '2013-01-01 01:01:07');

insert into record(journal,function, amount, timestamp) values (1, 'credit', 50.28, '2013-01-01 01:01:09');

insert into record(journal,function, amount, timestamp) values (2, 'credit', 44.42, '2013-01-01 01:01:11');

insert into record(journal,function, amount, timestamp) values (1, 'credit', 43.83, '2013-01-01 01:01:13');

insert into record(journal,function, amount, timestamp) values (2, 'credit', 10.40, '2013-01-01 01:01:15');

insert into record(journal,function, amount, timestamp) values (1, 'credit', 79.35, '2013-01-01 01:01:17');

insert into record(journal,function, amount, timestamp) values (1, 'credit', 79.02, '2013-01-01 01:01:19');

insert into record(journal,function, amount, timestamp) values (2, 'credit', 82.31, '2013-01-01 01:01:21');

我努力了:

mysql> select *

from record

where journal=1 and function='credit'

having sum(amount) >= 260

order by timestamp asc;

但是,该查询仅返回我想要的集合的第一行.所需的结果集如下所示:

+----+----------+--------+---------------------+---------+

| id | function | amount | timestamp | journal |

+----+----------+--------+---------------------+---------+

| 27 | credit | 81.27 | 2013-01-01 01:01:01 | 1 |

| 29 | credit | 95.62 | 2013-01-01 01:01:05 | 1 |

| 31 | credit | 50.28 | 2013-01-01 01:01:09 | 1 |

| 33 | credit | 43.83 | 2013-01-01 01:01:13 | 1 |

+----+----------+--------+---------------------+---------+

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值