Get users similar to your interest(s) from multiple tables in MySQL

Figure:-Interest_skill table

Figure:-Entity

Figure:- Interest table

Figure:- User table

select new_user.total_interest,firstname,surname,new_user.entity_source_id as user_id,new_user.entity_id,clg_under from `user`
inner join
(select entity_source_id,new_entity.entity_id,new_entity.total_interest as total_interest from `entity`
inner join
(SELECT entity_id,count(*) as total_interest
FROM
`interest`
inner join
`interest_skill`
on interest.interest_id=interest_skill.i_s_id
where interest_skill.entity_id <> '390'
AND interest.interest_id
IN (
    SELECT i_s_id FROM
`interest_skill` WHERE entity_id = '390'
)
group by entity_id order by count(*) desc) as new_entity
on new_entity.entity_id=entity.entity_id) as new_user
on new_user.entity_source_id=user.user_id
and user.session_end_year in (select session_end_year from user where user_id
in
(SELECT entity_source_id FROM
`entity` WHERE entity_id = '390'
)
)
and user.clg_under <> '0' in (select clg_under from user where user_id
in
(SELECT entity_source_id FROM
`entity` WHERE entity_id = '390'
)
)
and user.clg_post <> '0' in (select clg_post from user where user_id
in
(SELECT entity_source_id FROM
`entity` WHERE entity_id = '390'
)
)
and user.clg_higher <> '0' in (select clg_higher from user where user_id
in
(SELECT entity_source_id FROM
`entity` WHERE entity_id = '390'
)
)




In the above example :-

There are four tables
1.User(If the user is of UnderGraduated,PostGraduated or PHDorHigher,then the values of these columns will be non-empty respectively)

2.Entity(Every user has its own entity and the 'entity_source_id' column is the user_id(f_key referred to the 'User' table))

3.Interest(Interest stores Interest name and interest_id(which is an f_key in 'interest_skill' table and it also stores the entity_id('i_s_id' column)))

Note:-
I have used 'entity_id' '390' here as example

Comments