UserID A C D L P
admin 40 30 3 22 44
要更新的表tt2:
UserID UseType Score ScoreTime
admin A 45 2003-3-12
admin C 50 2003-3-12
admin D 26 2003-3-12
admin L 55 2003-3-12
admin P 26 2003-3-12
admin B 26 2003-3-12
aa B 24 2003-3-12
要用一条SQL语句把查询结果按UseType把相应的值加到tt2中来。
更新后表tt2的结果是:
UserID UseType Score ScoreTime
admin A 85 2003-3-12
admin C 80 2003-3-12
admin D 29 2003-3-12
admin L 77 2003-3-12
admin P 70 2003-3-12
admin B 26 2003-3-12
aa B 24 2003-3-12
11 个解决方案
#1
update tt2 set score=case usetype
when 'A' then score+bb.A
when 'C' then score+bb.C
when 'D' then score+bb.D
when 'L' then score+bb.L
when 'P' then score+bb.P
else score end
from tt1 a where tt2.userid=a.userid
when 'A' then score+bb.A
when 'C' then score+bb.C
when 'D' then score+bb.D
when 'L' then score+bb.L
when 'P' then score+bb.P
else score end
from tt1 a where tt2.userid=a.userid
#2
update tt2 set Score=Score+ ss
from tt1
(select userid, 'a' as UseType , a as ssfrom ttl union
select userid, 'c' as UseType , c as ss from ttl union
select userid, 'd' as UseType , d as ss from ttl union
select userid, 'l' as UseType , l as ss from ttl union
select userid, 'p' as UseType , p as ss from ttl ) a
where a.userid=tt2.userid and a.UseType =tt2.UseType
from tt1
(select userid, 'a' as UseType , a as ssfrom ttl union
select userid, 'c' as UseType , c as ss from ttl union
select userid, 'd' as UseType , d as ss from ttl union
select userid, 'l' as UseType , l as ss from ttl union
select userid, 'p' as UseType , p as ss from ttl ) a
where a.userid=tt2.userid and a.UseType =tt2.UseType
#3
update tt2 set score=case usetype
when 'A' then score+bb.A
when 'C' then score+bb.C
when 'D' then score+bb.D
when 'L' then score+bb.L
when 'P' then score+bb.P
else score end
from tt1 bb where tt2.userid=bb.userid
when 'A' then score+bb.A
when 'C' then score+bb.C
when 'D' then score+bb.D
when 'L' then score+bb.L
when 'P' then score+bb.P
else score end
from tt1 bb where tt2.userid=bb.userid
#4
tt1表结构问题。
A C D L P
是字段名UserID的值吗?
还是 A C D L P都是字段名?
A C D L P
是字段名UserID的值吗?
还是 A C D L P都是字段名?
#5
select * from tt1
select * from tt2
select tt2.UserID,tt2.UserType,
case UserType
when 'A' then tt2.Score + tt1.A
when 'C' then tt2.Score + tt1.C
when 'D' then tt2.Score + tt1.D
when 'L' then tt2.Score + tt1.L
when 'P' then tt2.Score + tt1.P
else
tt2.Score
end
,tt2.ScoreTime
from tt2,tt1
select * from tt2
select tt2.UserID,tt2.UserType,
case UserType
when 'A' then tt2.Score + tt1.A
when 'C' then tt2.Score + tt1.C
when 'D' then tt2.Score + tt1.D
when 'L' then tt2.Score + tt1.L
when 'P' then tt2.Score + tt1.P
else
tt2.Score
end
,tt2.ScoreTime
from tt2,tt1
#6
select tt2.UserID,tt2.UserType,
case UserType
when 'A' then tt2.Score + tt1.A
when 'C' then tt2.Score + tt1.C
when 'D' then tt2.Score + tt1.D
when 'L' then tt2.Score + tt1.L
when 'P' then tt2.Score + tt1.P
else
tt2.Score
end as Score
,tt2.ScoreTime
from tt2,tt1
case UserType
when 'A' then tt2.Score + tt1.A
when 'C' then tt2.Score + tt1.C
when 'D' then tt2.Score + tt1.D
when 'L' then tt2.Score + tt1.L
when 'P' then tt2.Score + tt1.P
else
tt2.Score
end as Score
,tt2.ScoreTime
from tt2,tt1
#7
CrazyFor(蚂蚁):
A、C 、D、L、P都是字段UseType的值,是一种标识?比如说D表示下载,P表示发布…
字段Score是表示各操作的得分总值,会不断的累加。
A、C 、D、L、P都是字段UseType的值,是一种标识?比如说D表示下载,P表示发布…
字段Score是表示各操作的得分总值,会不断的累加。
#8
表tt1其实是一个查询出来的结果集。
UserID A C D L P
admin 40 30 3 22 44
aa ……
bb ……
……
读出每个UserID的值后再往表tt2累加。
UserID A C D L P
admin 40 30 3 22 44
aa ……
bb ……
……
读出每个UserID的值后再往表tt2累加。
#9
难道我写错了吗?
#10
两个表的UserID需要一一对应吗?
#11
pengdali(大力)
A、C 、D、L、P不是字段名。
A、C 、D、L、P不是字段名。
#1
update tt2 set score=case usetype
when 'A' then score+bb.A
when 'C' then score+bb.C
when 'D' then score+bb.D
when 'L' then score+bb.L
when 'P' then score+bb.P
else score end
from tt1 a where tt2.userid=a.userid
when 'A' then score+bb.A
when 'C' then score+bb.C
when 'D' then score+bb.D
when 'L' then score+bb.L
when 'P' then score+bb.P
else score end
from tt1 a where tt2.userid=a.userid
#2
update tt2 set Score=Score+ ss
from tt1
(select userid, 'a' as UseType , a as ssfrom ttl union
select userid, 'c' as UseType , c as ss from ttl union
select userid, 'd' as UseType , d as ss from ttl union
select userid, 'l' as UseType , l as ss from ttl union
select userid, 'p' as UseType , p as ss from ttl ) a
where a.userid=tt2.userid and a.UseType =tt2.UseType
from tt1
(select userid, 'a' as UseType , a as ssfrom ttl union
select userid, 'c' as UseType , c as ss from ttl union
select userid, 'd' as UseType , d as ss from ttl union
select userid, 'l' as UseType , l as ss from ttl union
select userid, 'p' as UseType , p as ss from ttl ) a
where a.userid=tt2.userid and a.UseType =tt2.UseType
#3
update tt2 set score=case usetype
when 'A' then score+bb.A
when 'C' then score+bb.C
when 'D' then score+bb.D
when 'L' then score+bb.L
when 'P' then score+bb.P
else score end
from tt1 bb where tt2.userid=bb.userid
when 'A' then score+bb.A
when 'C' then score+bb.C
when 'D' then score+bb.D
when 'L' then score+bb.L
when 'P' then score+bb.P
else score end
from tt1 bb where tt2.userid=bb.userid
#4
tt1表结构问题。
A C D L P
是字段名UserID的值吗?
还是 A C D L P都是字段名?
A C D L P
是字段名UserID的值吗?
还是 A C D L P都是字段名?
#5
select * from tt1
select * from tt2
select tt2.UserID,tt2.UserType,
case UserType
when 'A' then tt2.Score + tt1.A
when 'C' then tt2.Score + tt1.C
when 'D' then tt2.Score + tt1.D
when 'L' then tt2.Score + tt1.L
when 'P' then tt2.Score + tt1.P
else
tt2.Score
end
,tt2.ScoreTime
from tt2,tt1
select * from tt2
select tt2.UserID,tt2.UserType,
case UserType
when 'A' then tt2.Score + tt1.A
when 'C' then tt2.Score + tt1.C
when 'D' then tt2.Score + tt1.D
when 'L' then tt2.Score + tt1.L
when 'P' then tt2.Score + tt1.P
else
tt2.Score
end
,tt2.ScoreTime
from tt2,tt1
#6
select tt2.UserID,tt2.UserType,
case UserType
when 'A' then tt2.Score + tt1.A
when 'C' then tt2.Score + tt1.C
when 'D' then tt2.Score + tt1.D
when 'L' then tt2.Score + tt1.L
when 'P' then tt2.Score + tt1.P
else
tt2.Score
end as Score
,tt2.ScoreTime
from tt2,tt1
case UserType
when 'A' then tt2.Score + tt1.A
when 'C' then tt2.Score + tt1.C
when 'D' then tt2.Score + tt1.D
when 'L' then tt2.Score + tt1.L
when 'P' then tt2.Score + tt1.P
else
tt2.Score
end as Score
,tt2.ScoreTime
from tt2,tt1
#7
CrazyFor(蚂蚁):
A、C 、D、L、P都是字段UseType的值,是一种标识?比如说D表示下载,P表示发布…
字段Score是表示各操作的得分总值,会不断的累加。
A、C 、D、L、P都是字段UseType的值,是一种标识?比如说D表示下载,P表示发布…
字段Score是表示各操作的得分总值,会不断的累加。
#8
表tt1其实是一个查询出来的结果集。
UserID A C D L P
admin 40 30 3 22 44
aa ……
bb ……
……
读出每个UserID的值后再往表tt2累加。
UserID A C D L P
admin 40 30 3 22 44
aa ……
bb ……
……
读出每个UserID的值后再往表tt2累加。
#9
难道我写错了吗?
#10
两个表的UserID需要一一对应吗?
#11
pengdali(大力)
A、C 、D、L、P不是字段名。
A、C 、D、L、P不是字段名。