问一sql,谢谢大家
表:table
id name sign1 sign2 userid money result
1 橘子 0.5 0 1 100
2 苹果 4.8 0.5 2 50
3 橘子 0.5 0 3 150
4 橘子 0 0.5 4 100
是这样.sign=0.5的有两个,sign2两0.5的有一个.我想把sign=0.5的钱平均分配给sign2=0.5的用户.然后在result显示.
------解决方案--------------------update 表名 set
result =
[money] + (select sum([money]) from 表名 where sign1=0.5)/(select count(*) from 表名 where sign2=0.5)
where sign2=0.5
------解决方案--------------------是不是這個意思?
update table set result = (select sum(money) from table where sign1 = 0.5)/ (select count(money) from table where sign2 = 0.5) where sign2 = 0.5
select name sign1 sign2 userid money result from table
------解决方案--------------------update 表名 set
result =
(select sum([money]) from 表名 where sign1=0.5)
/
(select sum([money]) from 表名 where sign2=0.5)
*
[money]
where sign2=0.5
------解决方案--------------------declare @number int
set @number1=0.5
set @number2=0.5
update 表名 set
result = (select sum([money]) from table where sign1=@number1)
/
(select count(*) from table where sign2=@number2)
where sign2=@number2
只要更改 @number1=0.5
@number2=0.5
中0.5就可以了,呵呵用局部变量搞!!