这个SQL怎么写

wolflove23 2010-07-18 09:50:14
表 1

id jh flag
1 A 0
2 B 0

表2

cid cjh flag

1 A1 1
1 A2 1


表1的ID 就是表2 的CID 当表2 的cid 的flag都是1时 表1 的第一行 flag 也是1
...全文
114 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
黄_瓜 2010-07-19
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 limpire 的回复:]
select id, jh, flag = case when not exists (select 1 from 表2 where cid=t.id and flag <> 1) then 1 else 0 end from 表1 t
[/Quote]
膜拜小楼前辈
kiruya0 2010-07-19
  • 打赏
  • 举报
回复
是表2update时候改表1?
用trigger
wmswxpmyl 2010-07-19
  • 打赏
  • 举报
回复
学习 学习!!!!!!!!!
999朵玫瑰 2010-07-19
  • 打赏
  • 举报
回复
学习了。。
feixianxxx 2010-07-18
  • 打赏
  • 举报
回复
--写个更新的
update tb1
set flag=1
from tb1 join tb2 on tb1.id=tb2.cid
where not exists(select * from tb2 where tb1.id=cid and flag<>1)
feixianxxx 2010-07-18
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 limpire 的回复:]

select id, jh, flag = case when not exists (select 1 from 表2 where cid=t.id and flag <> 1) then 1 else 0 end from 表1 t
[/Quote]
传说中的 楼大大
拜拜 ~

Limpire 2010-07-18
  • 打赏
  • 举报
回复
select id, jh, flag = case when not exists (select 1 from 表2 where cid=t.id and flag <> 1) then 1 else 0 end from 表1 t
rucypli 2010-07-18
  • 打赏
  • 举报
回复
select A.id,A.jh,case when n=0 then 0 else 1 end as flag
from
(
select A.id,A.jh,min(flag) n
from tb1 A,tb2 B
where A.id = B.cid
group by A.id,A.jh
)

34,587

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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