首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

一个比较麻烦的sql查询有关问题

2012-04-30 
一个比较麻烦的sql查询问题现在有表friend字段 ----id 主键userid 用户ID friendId 好友ID用户A 有好友 b

一个比较麻烦的sql查询问题
现在有表friend 
  字段 ----id 主键 userid 用户ID friendId 好友ID

用户A 有好友 b c d
现在我想为A推荐好友,需要查询 b 。c 。d的所有好友,但是,这些好友不能是A的好友该怎么办啊~~~

[解决办法]
也就是推荐给A的好友在A的好友列表中不存在


select * from tbl where not exists(子查询语句)
[解决办法]

SQL code
select * from friend  f  where userid in (select friendId  from friend where userid='a')and t.userid !='a'
[解决办法]
SQL code
select id,userid,friendid from  friend  where b.userid in (select friendid from tb where userid=a) and friendid not in (select friendid from tb where userid='a' 

热点排行