日期:2014-05-18  浏览次数:20428 次

一个比较麻烦的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'