免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1095 | 回复: 4

sql语句问题求助 [复制链接]

论坛徽章:
0
发表于 2004-06-22 18:00 |显示全部楼层
两个表t1、t2,t1的字段有a、b、c等,t2的字段有a、b、c等。
想进行这种操作:将t1中的a、b字段等于t2中的a、b字段的记录,
即t1.a=t2.a and t1.b=t2.b 的记录,将t1.c update成t2.c
请问sql语句如何写?

论坛徽章:
0
发表于 2004-06-22 18:25 |显示全部楼层

sql语句问题求助

  1. update t1 set c=
  2. (select c from t2 where t1.a=t2.a and t1.b=t2.b)
复制代码


此语句将把所有t1.c更新,如果t2表中不存在相应记录,则t1.c将置为空。如果不希望这样的话,可以这样改:
  1. update t1 set c=
  2. (select c from t2 where t1.a=t2.a and t1.b=t2.b)
  3. where c in (select c from t2)
复制代码

写的不是很好,大家多提意见。

论坛徽章:
0
发表于 2004-06-22 21:33 |显示全部楼层

sql语句问题求助

update t1 ,t2 set t1.c=t2.c
where t1.a=t2.a and t1.b=t2.b;
可以吗?

论坛徽章:
0
发表于 2004-06-23 00:11 |显示全部楼层

sql语句问题求助

[quote]原帖由 "amiescort"][/quote 发表:

多谢了!
不过,代码2中的
  1. where c in (select c from t2)
复制代码

应该是"where a in (select a from t2)"吧?

论坛徽章:
0
发表于 2004-06-23 09:48 |显示全部楼层

sql语句问题求助

应该是where a||b in (select a||b from t2)
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP