数据库简单问题

Jangle_ 2017-01-05 07:44:45
1)问题描述:假设有A、B两个人同时用同一个账号分别下金额为50和60的订单,但是账号余额只有100,如果不做处理,他们下订单都会成功。问:做一个怎样的简单处理。
2)问题说明:可简单的文字说明或者伪代码。订单成功是依据账号余额是否不小于订单价格。此问题重点在于数据库并且执行起来简单、高效。
...全文
393 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
卖水果的net 版主 2017-01-05
  • 打赏
  • 举报
回复

set nocount on
create table test(id int, num int)
go
insert into test values(1,100),(2,100)
go
update test set num = num - 60 where id = 1 and num >=60
print '1.1 先减掉 60 ,更新的行数:' + ltrim(@@rowcount)
update test set num = num - 50 where id = 1 and num >=50
print '1.2 先减掉 50 ,更新的行数:' + ltrim(@@rowcount)
go

update test set num = num - 50 where id = 2 and num >=50
print '2.1 先减掉 50 ,更新的行数:' + ltrim(@@rowcount)
update test set num = num - 60 where id = 2 and num >=60
print '2.2 先减掉 60 ,更新的行数:' + ltrim(@@rowcount)
go

select * from test 
go
drop table test
go

1.1 先减掉 60 ,更新的行数:1
1.2 先减掉 50 ,更新的行数:0


2.1 先减掉 50 ,更新的行数:1
2.2 先减掉 60 ,更新的行数:0


id          num
----------- -----------
1           40
2           50


Jangle_ 2017-01-05
  • 打赏
  • 举报
回复
能说得具体点吗,没数据库基础,看影响行数,为0表示余额不足是啥意思
卖水果的net 版主 2017-01-05
  • 打赏
  • 举报
回复
前些日子,楼主是不是问过这个问题? 用户1: update t set je = je -50 where userid = ? and je >= 50 用户2: update t set je = je -50 where userid = ? and je >= 60 两个语句,都会成功,看影响的行数,为 0 的表示没有余额不足

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧