Good day guys. For this problem i need to get the user1 from table "follows" that follows user2 if user2 follows user1. It's kinda tricky. I did it with not exists and not in but I get all the names from user1 column.
For example:
Andreas follows Katerina but Katerina doesn't follow Andreas. So Andreas is not in what I want. But Aris follows Anna and Anna follows Aris. So we get Aris as result.
The thing is that I don't get such results and I get all the names.
Follows
table:
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
table:
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');
The results I want to get are the following:
user1
------
Aris
Aris
Aris(because 3 of the people that he follows, follow him too)
Anna
Maria
Panos
Yannis
My code:
/* 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);
Thanks in advance!
If you want pairs that following each other, how about:
The last condition just removes duplicates, so a pair of names only appears once in the result set.