Sql不存在而不在语句中

时间:2014-12-01 17:25:03

标签: sql not-exists notin

大家好。对于这个问题,我需要从表中获取user1"关注"如果user2跟随user1,则跟随user2。这有点棘手。我做的不存在而不存在,但我从user1列获得所有名称。

例如:

安德烈亚斯跟随凯特琳娜,但卡特琳娜并没有跟随安德烈亚斯。所以安德烈亚斯不是我想要的。但阿里斯跟随安娜和安娜跟随阿里斯。所以我们得到了阿里斯的结果。

问题是我没有得到这样的结果而且我得到了所有的名字。

Follows表:

                           user1_name    user2_name
insert into follows values('Andreas',   'Katerina');
insert into follows values('Aris',  'Andreas');
insert into follows values('Aris',  'Anna');
insert into follows values('Aris',  'Kostas');
insert into follows values('Aris',  'Panos');
insert into follows values('Aris',  'Yannis');
insert into follows values('Anna'   ,'Aris');
insert into follows values('Anna',  'Maria');
insert into follows values('Anna',  'Panos' );
insert into follows values('Dimitris',  'Maria');
insert into follows values('Dimitris',  'Anna');
insert into follows values('Kostas' ,'Andreas');
insert into follows values('Kostas',    'Panos');
insert into follows values('Kostas',    'Katerina');
insert into follows values('Maria', 'Yannis');
insert into follows values('Maria', 'Kostas');
insert into follows values('Maria', 'Anna');
insert into follows values('Maria', 'Aris');
insert into follows values('Maria', 'Panos');
insert into follows values('Panos','Andreas');
insert into follows values('Panos', 'Aris');
insert into follows values('Petros',    'Andreas');
insert into follows values('Yannis',    'Aris');
insert into follows values('Yannis',    'Andreas');

User information表:

insert into user_inf values('Andreas',  'Martiou 25',   '1990-12-02');
insert into user_inf values('Aris', 'Papandreou 10',    '1987-03-12');
insert into user_inf values('Anna', 'Aiakidon 20',  '1989-07-15');
insert into user_inf values('Dimitris', 'Dodonis 3',    '1992-09-07');
insert into user_inf values('Katerina', '28 Oktobriou 4',   '1993-01-09');
insert into user_inf values('Kostas','Kasioumi 3',  '1992-12-12');
insert into user_inf values('Maria',    'Kalari 8', '1993-08-31');
insert into user_inf values('Petros',   'Panepistimiou 9',  '1992-04-15');
insert into user_inf values('Panos',    'Aneksartisias 13', '1991-05-27');
insert into user_inf values('Yannis',   'Ithakis 20',   '1993-07-03');

我想得到的结果如下:

user1
------
Aris
Aris
Aris(because 3 of the people that he follows, follow him too)
Anna
Maria
Panos
Yannis

我的代码:

/*  not exists.*/
select distinct Fol.user1_name
from follows as Fol,user_inf as Usr
where not exists
                        (select distinct F.user2_name,F.user1_name
                          from follows as F, user_inf as U
                          where Fol.user2_name = F.user1_name AND Fol.user1_name = F.user2_name AND F.user1_name = Usr.name AND Fol.user1_name = U.name );

/*  not in.*/
select distinct Fol.user2_name
from follows as Fol,user_inf as Usr
where Fol.user1_name not in
                        (select distinct F.user1_name,F.user2_name
                          from follows as F, user_inf as U
                          where Fol.user2_name = F.user1_name AND Fol.user1_name <> F.user2_name AND F.user1_name = Usr.name AND Fol.user2_name <> U.name);

提前致谢!

2 个答案:

答案 0 :(得分:1)

如果你想要相互跟随的对,那么:

select f1.*
from follows f1 join
     follows f2
     on f1.user1_name = f2.user2_name and
        f1.user2_name = f1.user1_name and
        f1.user1_name < f1.user2_name;

最后一个条件只是删除了重复项,因此一对名称只在结果集中出现一次。

答案 1 :(得分:0)

自我加入怎么样?

这应该会产生一个数据集,用户可以互相跟随源用户的用户信息。

SELECT F.user1_name, F.user2_name, u.*
FROM follows F
INNER JOIN follows F2
  on F.user2_name = F2.User1_name
INNER JOIn user_inf U
  on F.user1.name U.name

根据集合考虑关系数据 你想要设置1和设置的数据的交集是彼此的追随者。

我假设人们不跟随自己。 user1_name是唯一的

现在,如果您需要删除该集合,以便仅列出一次关系...... 添加到第一个内部联接

on F.user1_name > F.user1_name