第一个表 
表名  user 
字段分别为  id(递增id)  username(用户名)  groupid(用户群id)
             1                我是阿一            1 
             2                我是阿二            1
             3                我是阿三            2
             4                我是阿四            3
------------------------------------------------
第二个表
表名  message
字段分别为   id(递增id)  touserid(接收人id)     title(内容)  
--------------------------------------------------
      我向用户群的id为 1,3 发送信息,传递参数为用户群id (转下行)
       
         GroupIdList = 1,3我选择的是群id,我怎么在message(第二个表)插入其下属的用户id呢?我需要的效果如下表:message
字段   id(递增id)  touserid(接收人id)       title(内容)  
        1                 1                  我是内容
        2                 2                  我是内容
        3                 4                  我是内容请高手指点。感谢。我想了一上午想不出来。

解决方案 »

  1.   

    insert into message(touserid, title)
    select id, '我是内容'
    from user
    where charindex(',' + @GroupIdList + ',', ',' + cast(groupid as varchar) + ',') > 0
      

  2.   

    create table uuser (id int, username varchar(100), groupid int)
    insert uuser select 1, '我是阿一', 1 
    union all select 2, '我是阿二' ,1
    union all select 3 ,'我是阿三', 2
    union all select 4, '我是阿四', 3create table message(id int identity(1,1), touserid int, title varchar(100))Create proc pr_ttt
    @GroupIdList varchar(10)
    as
    declare @a varchar(199)
    set @GroupIdList=@GroupIdList+','
    truncate table message
    while charindex(',',@GroupIdList)>0
    begin
    insert message select id,username from uuser where groupid=cast(left(@GroupIdList,charindex(',',@GroupIdList)-1) as int)
    set @GroupIdList=right(@GroupIdList,len(@GroupIdList)-charindex(',',@GroupIdList))
    end
    select * from messageexec pr_ttt '1,3'
      

  3.   

    楼上的使用charindex有点问题
    当数据多一点的时候,1在11,12中的charindex也是大于0的
    所以还需要改进有两个方法
    把GroupIdList字符串拆分或者把groupid的格式改为100001或者字符串的0001等等
      

  4.   

    create table uuser (id int, username varchar(100), groupid int)
    insert uuser select 1, '我是阿一', 1 
    union all select 2, '我是阿二' ,1
    union all select 3 ,'我是阿三', 2
    union all select 4, '我是阿四', 3create table message(id int identity(1,1), touserid int, title varchar(100))
    insert message select id,'我的内容' from uuser where charindex(','+ cast(groupid as  varchar) + ',',','+ '1,3' + ',')>0select * from message
      

  5.   

    create table uuser (id int, username varchar(100), groupid int)
    insert uuser select 1, '我是阿一', 1 
    union all select 2, '我是阿二' ,1
    union all select 3 ,'我是阿三', 2
    union all select 4, '我是阿四', 3
    union all select 5, '我是阿四', 3
    union all select 5, '我是阿四', 3create table message(id int identity(1,1), touserid int, title varchar(100))insert message(touserid,title)
    select [id],'123' from uuser where groupid in (1,3)
    select * from messagedrop table uuser
    drop table message
      

  6.   

    insert into message(touserid,title)
    select id,'我的内容'
    from user
    where charindex(cast(id as varchar),'1,3')
    -->此法结合你的程序具体操作。。
      

  7.   

    我写了个比较笨的方法如下,有更好的方法希望贴出来
    Set NoCount ONGo
    if Exists(Select Table_Name From Information_schema.Tables where Table_Name = 'MyUser')
       Drop Table MyUserif Exists(Select Table_Name From Information_Schema.Tables Where Table_Name = 'Message')
       Drop Table Message
    Go
    Create Table MyUser
    (
    UserID     int IDENTITY(1,1),
    UserName   Varchar(20),
    GroupID    Varchar(20) 
    )Create Table Message
    (
    MessageID int IDENTITY(1,2),
    ToUserID  varchar(20),
    Title     Varchar(200)
    )GoInsert Into MyUser(UserName,GroupID)
    Select '阿1','1' UNION
    Select '阿2','1' UNION
    Select '阿3','2' UNION
    Select '阿4','3' Declare  @sTempList varchar(50)
    Declare  @sTemp varchar(5)
    set @sTempList =  '1,3,'
    set @sTemp =''While CharIndex(',',@sTempList)>0
    Begin
     set @sTemp = substring(@sTempList,1,CharIndex(',',@sTempList)-1)
     set @sTempList = substring(@sTempList,CharIndex(',',@sTempList)+1,len(@sTempList))
    Insert Into Message(ToUserID)
    Select UserName From MyUser where CharIndex(@sTemp,GroupID)>0
    End
      

  8.   

    十分感谢楼上的几位朋友的指教。今天又学到了些东西。原来sql还可以这样用。呵呵。感激。我已经解决了我的问题。不过有些我还是看不明白。例如楼上的(还有几位都是,怎么全都是在查询分析器运行的呢?我不懂。望各位继续赐教)。我学sql没多久。
      

  9.   

    insert message(touserid,title)
    select [id],'内容' from user where groupid in (1,3)不知道你的内容怎么得到?
      

  10.   

    declare @aa varchar(80)
    set @aa='1,3,7'
    select @aa where (@aa like cast(1 as varchar)+',%' or @aa like '%,'+cast(1 as varchar)+',%' or @aa like '%,'+cast(1 as varchar))
      

  11.   

    declare @GroupIdList varchar(80)
    set @GroupIdList='1,3,7'
    select * from user 
    where (@GroupIdList like cast(groupid as varchar)+',%' or @GroupIdList like '%,'+cast(groupid as varchar)+',%' or @GroupIdList like '%,'+cast(groupid as varchar))