php - How to find intersect rows when condition depend on some columns in one table -
table subscribe
subscriber | subscribeto (columns) 1 | 5 1 | 6 1 | 7 1 | 8 1 | 9 1 | 10 2 | 5 2 | 6 2 | 7
there 2 users have id 1 , 2. subscribe various user , inserted these data table subscribe. column subscriber indicates subscriber , column subscribeto indicates they've subscribe to. above table can conclude that;
user id=1 subscribed 6 users
user id=2 subscribed 3 users
i want find mutual of subscription (like facebook mutual friends)
user 1 subscribe user 5,6,7,8,9,10
user 2 subscribe user 5,6,7
so, mutual subscription of user 1 , 2 are: 5,6,7
and i'm trying create sql statement..
give user table sql statement , think can use subscribe table can't figure out.
table user
userid (columns) 1 2 3 ... ...
sql
"select * user (select count( 1 ) subscribe subscriber = '1' , subscribeto = user.userid) , (select count( 1 ) subscribe subscriber = '2' , subscribeto = user.userid);"
this sql can work correctly, slow thousands of columns. please provide better sql me, thanks.
drop table if exists my_table; create table my_table (subscriber int not null ,subscribeto int not null ,primary key(subscriber,subscribeto) ); insert my_table values (1,5), (1,6), (1,7), (1,8), (1,9), (1,10), (2,5), (2,6), (2,7); select x.subscribeto mutual_friends my_table x join my_table y on y.subscribeto = x.subscribeto x.subscriber = 1 , y.subscriber = 2; +----------------+ | mutual_friends | +----------------+ | 5 | | 6 | | 7 | +----------------+
Comments
Post a Comment