人生不设限

导航

SQL编写-谁和谁是好朋友

problem:

用户表
姓名
张三
李四  
王五
 
   
好友表  
姓名 好友姓名
张三 李四
张三 王五
李四 张三

 用户表里面姓名必须在好友表里面互加为好友,请问如何找出还没有加为好友的姓名,不允许加自己为好友。

answer:

select t.name,t2.name from usr t,usr t2
 where t.name<>t2.name
 and not exists
(select null from usr_frds t3 where t3.name=t.name and t3.frd=t2.name);

 

posted on 2016-04-13 15:57  风的_理想  阅读(167)  评论(0编辑  收藏  举报