免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2031 | 回复: 3
打印 上一主题 下一主题

请教一条SQL的UPdata语句 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-12-16 21:00 |只看该作者 |倒序浏览
a        b        c
1        1        7
2        1       
3        1       
4        1       
5        2       
6        2        8
7        2       
8        2       
9        3        9
10        3       
11        3       
12        3       
13        3       
14        3       
15        4        10
16        4       
17        4       
以上是一个测试数据
现在想更新C列,根据B列的分组和C列的值的判断,更新C列没有值的项,更新的值是每组中C列的值。

论坛徽章:
0
2 [报告]
发表于 2010-12-17 11:09 |只看该作者
select distinct b from table
然後用個遊標循環查出來的b
for each b
decalre @c
select @c=c form table where c<>\'\' and b=循環的值
update C=@c where b=循環的值

论坛徽章:
0
3 [报告]
发表于 2011-01-09 09:33 |只看该作者
update A  set c=(select distinct e.c from A e where e.b=A.b where e.c is not null) where c is null

论坛徽章:
0
4 [报告]
发表于 2011-01-11 18:47 |只看该作者
本帖最后由 joeng165 于 2011-1-11 18:49 编辑

--on Microsoft SQL
update  t
set     c = d.c
from    t
        left outer join t d on t.b = d.b
where   t.c is null
        and d.c is not null
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP