首页 新闻 论坛 群组 Blog 文档 下载 读书 Tag 网摘 搜索 .NET Java 游戏 视频 人才 外包 培训 数据库 书店 程序员
中国软件网
欢迎您:游客 | 登录 注册 帮助
  • 100分求一个查找所有好友的存储过程!!!!!!!!!!!!!!!!!!!!!!! [已结贴,结贴人:txgaozhao]
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • txgaozhao
    • 等级:
    发表于:2008-05-11 23:18:14 楼主
    我有一个用户表
    userid int 用户的id字段
    username varchar 用户的昵称
    fids varchar 用户的好友字段集合,用逗号隔开,比如“132,243,114,252,182”

    即一个用户加的好友都记录在字段fids里,好友id之间用逗号隔开
    现在我要获取某个用户的所有好友信息,请问我该怎么写存储过程?就比如说用户a要获取他的所有好友的昵称列表,请问怎么实现。假设用户a的userid=100
    100  修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • DBKING
    • 等级:
    发表于:2008-05-11 23:22:561楼 得分:80
    declare @fids varchar(max)

    select @fids=fids from tablename where userid=100

    exec ('select * from table where ' + @fids)
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-11 23:23:082楼 得分:0
    select * from haoyoutable where fid in(select fids from userinfo where userid=100)
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • DBKING
    • 等级:
    发表于:2008-05-11 23:31:403楼 得分:0
    引用 2 楼 cfreez 的回复:
    select * from haoyoutable where fid in(select fids from userinfo where userid=100)

    貌似in里面必须是字符串或者是向量阿。老大,呵呵。
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • txgaozhao
    • 等级:
    发表于:2008-05-11 23:38:024楼 得分:0
    引用 2 楼 cfreez 的回复:
    select * from haoyoutable where fid in(select fids from userinfo where userid=100)

    不要误导我哟,要是真的能这样子用的话,就好了
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-11 23:38:225楼 得分:0
    1.要么用in
    SQL code
    select * from haoyoutable where fid in(select fids from userinfo where userid=100)

    2.要么就拆分字符串,
    看看这个帖子
    http://topic.csdn.net/u/20080106/23/a9beba18-67b0-47c7-bf34-05afbdeae1a0.html
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • txgaozhao
    • 等级:
    发表于:2008-05-11 23:39:106楼 得分:0
    引用 1 楼 DBKING 的回复:
    declare @fids varchar(max)

    select @fids=fids from tablename where userid=100

    exec ('select * from table where ' + @fids)

    我看不懂诶?exec ('select * from table where ' + @fids)这个是什么意思啊?
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-11 23:40:487楼 得分:0
    引用 1 楼 DBKING 的回复:
    declare @fids varchar(max)

    select @fids=fids from tablename where userid=100

    exec ('select * from table where ' + @fids)
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • txgaozhao
    • 等级:
    发表于:2008-05-11 23:43:038楼 得分:0
    引用 5 楼 hery2002 的回复:
    1.要么用in
    SQL codeselect*fromhaoyoutablewherefidin(selectfidsfromuserinfowhereuserid=100)
    2.要么就拆分字符串,
    看看这个帖子
    http://topic.csdn.net/u/20080106/23/a9beba18-67b0-47c7-bf34-05afbdeae1a0.html

    用in可以吗?字段fids是好友id的一个集合的哦,就想这个“132,243,114,252,182” ,只是好友之间用逗号隔开而已。用in不行的吧?
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-11 23:46:339楼 得分:0
    用in可以的,楼主试试就知
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • txgaozhao
    • 等级:
    发表于:2008-05-11 23:51:4610楼 得分:0
    select * from Usertable where CAST(UserId AS VARCHAR(20)) in
    (select FIds from Usertable where UserId= 132)
    刚试了,不行的啊。
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-11 23:51:5811楼 得分:20
    引用 3 楼 DBKING 的回复:
    引用 2 楼 cfreez 的回复:
    select * from haoyoutable where fid in(select fids from userinfo where userid=100)

    貌似in里面必须是字符串或者是向量阿。老大,呵呵。

    int类型可以使用的,举例:
    SQL code
    declare @tb table (id int, cname nvarchar(20), ctype nvarchar(20), total int) insert into @tb select 1, 'a', 'aa', 0 union all select 1 ,'a1', 'aa', 2 union all select 1 ,'a2', 'aa', 0 union all select 1 ,'b', 'bb', 0 union all select 1 ,'b1', 'bb', 1 union all select 1 ,'b2', 'bb', 2 union all select 2 ,'a', 'aa', 0 union all select 2 ,'a1', 'aa', 6 union all select 2 ,'a2', 'aa', 4 union all select 2 ,'b', 'bb', 0 union all select 2 ,'b1', 'bb', 5 union all select 2 ,'b2', 'bb', 4 union all select 3 ,'a', 'aa', 0 union all select 3 ,'a1', 'aa', 3 union all select 3 ,'a2', 'aa', 0 union all select 3 ,'b', 'bb', 0 union all select 3 ,'b1', 'bb', 2 union all select 3 ,'b2', 'bb', 4 select * from @tb where id in (1,2) /* id cname ctype total ----------- -------------------- -------------------- ----------- 1 a aa 0 1 a1 aa 2 1 a2 aa 0 1 b bb 0 1 b1 bb 1 1 b2 bb 2 2 a aa 0 2 a1 aa 6 2 a2 aa 4 2 b bb 0 2 b1 bb 5 2 b2 bb 4 */
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-11 23:52:2812楼 得分:0
    SQL code
    create proc wsp @userid int as select a.* from 用户表 a,用户表 b where b.userid=@userid and charindex(','+ltrim(a.userid)+',',','+b.fids+',')>0 go --调用: exec wsp 100
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-11 23:53:3413楼 得分:0
    引用 10 楼 txgaozhao 的回复:
    select * from Usertable where CAST(UserId AS VARCHAR(20)) in 
    (select FIds from Usertable where UserId= 132)
    刚试了,不行的啊。

    你为啥要转换啦?
    SQL code
    select * from Usertable where [UserId] in (select FIds from Usertable where UserId= 132)
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • DBKING
    • 等级:
    发表于:2008-05-11 23:57:4714楼 得分:0
    引用 6 楼 txgaozhao 的回复:
    引用 1 楼 DBKING 的回复:
    declare @fids varchar(max)

    select @fids=fids from tablename where userid=100

    exec ('select * from table where ' + @fids)

    我看不懂诶?exec ('select * from table where ' + @fids)这个是什么意思啊?


    写错了,ha ,你的fids里面不是 1,2,3这种形式么?
    exec ('select * from table where fids in (''' + @fids+'')')
    有问题可以直接找我交流 qq 86053555

    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • skyaspnet
    • 等级:
    发表于:2008-05-12 00:00:2415楼 得分:0
    userid int 用户的id字段
    username varchar 用户的昵称
    fids varchar 用户的好友字段集合,用逗号隔开,比如“132,243,114,252,182”
    SQL code
    create procedure friends @uid int as select * from haoyoutable where userid in (select fids from haoyoutable where uid=@uid)
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • txgaozhao
    • 等级:
    发表于:2008-05-12 00:22:0916楼 得分:0
    引用 11 楼 hery2002 的回复:
    引用 3 楼 DBKING 的回复:
    引用 2 楼 cfreez 的回复:
    select * from haoyoutable where fid in(select fids from userinfo where userid=100)

    貌似in里面必须是字符串或者是向量阿。老大,呵呵。

    int类型可以使用的,举例:
    SQL codedeclare@tbtable(idint, cnamenvarchar(20), ctypenvarchar(20), totalint)insertinto@tbselect1,'a','aa',0unionallselect1,'a1','aa',2unionallselect1,'a2','aa',0unionallselect1,'b',…

    不行的啊,我的不是int类型,是char类型
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • txgaozhao
    • 等级:
    发表于:2008-05-12 00:23:2817楼 得分:0
    引用 13 楼 hery2002 的回复:
    引用 10 楼 txgaozhao 的回复:
    select * from Usertable where CAST(UserId AS VARCHAR(20)) in
    (select FIds from Usertable where UserId= 132)
    刚试了,不行的啊。

    你为啥要转换啦?
    SQL codeselect*fromUsertablewhere[UserId]in(selectFIdsfromUsertablewhereUserId=132)

    因为FIds是char类型啊,而UserId是int类型,不转换会报错的
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-12 07:26:2418楼 得分:0
    SQL code
    CREATE PROCEDURE procedureName ( @userid int ) AS BEGIN declare @sql varchar(max) --sql查询语句 declare @ids varchar(max) --用户id集合 select @ids=fids from tableName where userid=@userid set @sql='select * from tableName where userid in (' + @ids + ')'; exec(@sql) end
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-12 08:36:3519楼 得分:0
    该回复于2008-05-16 16:35:46被版主删除
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-12 09:28:0820楼 得分:0
    顶下
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • xp1056
    • 等级:
    发表于:2008-05-12 13:08:4021楼 得分:0
    SQL code
    declare @table table (userid int, username varchar(20),fids varchar(200)) insert into @table select 1000 ,'a0', '1001' union select 1001 ,'a1', '1002' union select 1002 ,'a2', '1000,1001,1003' union select 1003 ,'a3', '1002,1000' --将条件下的fids存储在一个临时变量中 declare @temp varchar(200) set @temp=(select fids from @table where userid=1002 ) --定义一个临时表 declare @tb table(userid int) --将临时变量拆借,放入临时表中 while charindex(',',@temp)>0 BEGIN insert into @tb select CAST ( substring(@temp,1,charindex(',',@temp)-1) AS int ) set @temp=substring(@temp,charindex(',',@temp)+1,len(@temp)) END insert into @tb select @temp --得到最终信息 select * from @table where userid in ( select userid from @tb )
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • xp1056
    • 等级:
    发表于:2008-05-12 13:10:1622楼 得分:0
    晕,中午吃了顿饭,帖子就给结了
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • khijfv2008
    • 等级:
    发表于:2008-05-14 12:31:2923楼 得分:0
    来迟了没接到分,呜呜
    修改 删除 举报 引用 回复

    网站简介广告服务网站地图帮助联系方式诚聘英才English 问题报告
    世纪乐知(北京)网络技术有限公司 版权所有 京 ICP 证 020026 号
    Copyright © 2000-2007, CSDN.NET, All Rights Reserved