求一条SQL语句的写法!!! 谢谢!!!

imports 2002-11-07 03:30:04
我想要一条SQL语句!!!

有一张表,里面有 GroupName(组名),NumInGroup(在组里的编号<3位的数字>)等字段

我要update这一张表。但是,要有条件的update

例如:我将qq这条记录加入到GroupName(组名)为A的组别中,

qq这条记录的NumInGroup(在组里的编号)为'123',首先在A组中,搜索!!!

如果在A组中的所有原记录的NumInGroup(在组里的编号)中,有与qq的NumInGroup(在组里的编号) 《《相同》》的话,就在原来的组A的所有记录的NumInGroup(在组里的编号)中找最大的NumInGroup,并在这个NumInGroup的基础上加1!!!

使它做为qq记录的新NumInGroup!!!

使qq记录的NumInGroup不与原A组的记录的NumInGroup重叠!!!

我要这条SQL语句的写法!!!

谢谢!!!
...全文
92 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
SilveryFox 2002-11-07
  • 打赏
  • 举报
回复
gz
CSDNM 2002-11-07
  • 打赏
  • 举报
回复
update x
set GroupName='A',NumInGroup=case
when exists (select * from tablename where GroupName='A' and NumInGroup=x.NumInGroup) then isnull((select max(NumInGroup) from tablename where GroupName='A'),0)+1
else a.NumInGroup
end
from tablename x
where x.name='qq'
蓝天 2002-11-07
  • 打赏
  • 举报
回复
看看这里,是否对你有帮助
这样的要求用触发器实现很方便

http://expert.csdn.net/Expert/topic/1131/1131325.xml?temp=.5935022
hjhing 2002-11-07
  • 打赏
  • 举报
回复
insert into table(GroupName,NumInGroup)
values(@qq,
case
when exists(select numIngroup from QQtable where groupName='A' and NumInGroup =@numInGroup) then @numInGroup
else (select Max(NumInGroup)+1 from table where groupName='A')
)


2:

if exists(select numIngroup from QQtable where groupName='A' and NumInGroup =@numInGroup)
select @numInGroup=max(NumInGroup)+1 from QQtable

insert into QQtable
values
(@qq,@numInGroup)


2:



wyj581 2002-11-07
  • 打赏
  • 举报
回复
up
  • 打赏
  • 举报
回复
when (select Count(*)from table where groupName='A' and NumInGroup =QQ.numInGroup)>0 then (select Count(*)from table where groupName='A')+1
From QQTable

修改為
when (select Count(*)from table where groupName='A' and NumInGroup =QQ.numInGroup)>0 then (select max(NumInGroup)from table where groupName='A')+1 END
From QQTable
CrazyFor 2002-11-07
  • 打赏
  • 举报
回复
insert into table(GroupName,NumInGroup)
Select 'QQ',
Case
when (select Count(*)from table where groupName='A' and NumInGroup =QQ.numInGroup)=0 then QQ.numInGroup
when (select Count(*)from table where groupName='A' and NumInGroup =QQ.numInGroup)>0 then (select Count(*)from table where groupName='A')+1
From QQTable
senzz 2002-11-07
  • 打赏
  • 举报
回复
问题是补是这样?

如有相同,则查询最大的编号,然后加1,INSERT
否则直接INSERT

34,576

社区成员

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

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