求解SQL语句

wq58 2003-10-18 01:48:34
假设有一主表 Table(TTID,IFOVER)
从表 DetailTable(DETAILID,TTID,...,IFOVER)

Table: DetailTable:

TTID IFOVER DetailID, TTID,..IFOVER
1001 0 1 1001 0
2 1001 1

通过TTID字段连接,IFOVER字段为逻辑值(0,1),要实现当DetailTable表中的IFOVER
值全部为1时将Table表的IFOVER值赋为1.谢谢
...全文
43 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wq58 2003-10-18
  • 打赏
  • 举报
回复
补充一点,我目的是要将后面Select语句查找出来的记录的IFOVER字段=1;
wq58 2003-10-18
  • 打赏
  • 举报
回复
我执行这样一条语句
'UPDATE WLSGDDETAIL SET IFOVER=1 Where exists (select D.NO_WLSGDDETAILID From WLSGDDETAIL D,WLSGD W Where W.IFCG=1 AND D.STRSHOPID='SH0001' AND D.NO_SGDH=W.NO_SGDH) '

其中select语句的结果为47,但结果却把表里的记录全部更新了,为什么呀?
我Select的结果可能是一个集合,又不能用=,该怎么处理呢?请大家帮帮忙啊!
wq58 2003-10-18
  • 打赏
  • 举报
回复
谢谢各位
podded 2003-10-18
  • 打赏
  • 举报
回复
哦,beckhambobo(beckham):需要更新三行 是相对于我的实验数据说的,不好意思啊。

podded 2003-10-18
  • 打赏
  • 举报
回复
welyngj(平平淡淡):如果有一个b.ifover=0则所有的都不能更新。

beckhambobo(beckham):需要更新三行

SQL> select * from test;

TTID IF
---- --
1001 0
1002 0
1003 0

SQL> select * from detailtest;

D TTID IF
- ---- --
1 1001 0
2 1001 1
3 1002 1
4 1002 1
5 1003 0

SQL> update test a set a.ifover='1' where a.ttid not in
2 (select distinct(b.ttid) from test a ,detailtest b where a.ttid=b.ttid and b.ifover='0');

已更新 1 行。

SQL> select * from test;

TTID IF
---- --
1001 0
1002 1
1003 0


beckhambobo 2003-10-18
  • 打赏
  • 举报
回复
update table a set ifover=(select min(IFOVER) from detailtable b where a.ttid=b.ttid);
welyngj 2003-10-18
  • 打赏
  • 举报
回复
update table set ifover=1 where not exists(select * from table a,detailtable b
where a.ttid=b.ttid and b.ifover=0)
21ithorse 2003-10-18
  • 打赏
  • 举报
回复
Select temp.iForver from (Select TTID,distinct(Ifover) From DetailTable ) temp,
DetailTable t where t.TTId=temp.TTid
判断Ifover值是否为I如为1那就可以更新主表如果是0或0,1哪就为0

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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