求语句:
表结构:id,type,val
数据: 1 a 2
2 a 23
3 b 22
4 a -5
select `id`,`val`,`余额`??? where type='a';
结果要求:
1 a 2 2
2 a 23 25
4 a -5 20
可用存储过程但不能用临时表
表结构:id,type,val
数据: 1 a 2
2 a 23
3 b 22
4 a -5
select `id`,`val`,`余额`??? where type='a';
结果要求:
1 a 2 2
2 a 23 25
4 a -5 20
可用存储过程但不能用临时表
解决方案 »
- 语句中单引号的问题
- 我有一主和一从机器,我想让从机只复制主机12小时前的数据,应该怎么办?
- 请教排序问题,谢谢
- 要写一份mysql数据库维护文档,大家给点建议吧,谢谢!
- 关键词排序,做过的请教请教!
- Mysql连接问题,求助!!
- 在检索数据库数据时,提示“Can't create/write to file 'c:\windows\temp\#sql_7f0_0.MYI'错误。。。
- mysql的不正常的日期格式转换
- 求助:MYSQL出错:java.sql.SQLException: No operations allowed after connection closed
- 数据库拒绝访问
- unix_timestamp()转换问题
- update 只更新改变的数据,有没有简便的方法?
from yourTable t
where type='a'
select a.id,a.type,a.value,sum(b.val)
from yourTable a ,yourTable b
where a.type=b.type and a.id>=b.id
and a.type='a'
from t b
group by id
order by word,id
+------+------+------+------+
| id | word | val | 余额 |
+------+------+------+------+
| 1 | a | 2 | 2 |
| 2 | a | 23 | 25 |
| 4 | a | -5 | 20 |
| 3 | b | 22 | 22 |
+------+------+------+------+