I have two tables in my database
users table :
+-----------+---------------------+---------+---------+
| id | username | article | date |
+-----------+---------------------+---------+---------+
| 1 | max | 2 |392185767|
+-----------+---------------------+---------+---------+
| 2 | alex | 3 |392333337|
+-----------+---------------------+---------+---------+
user_specialtys table :
+-----------+---------------------+
| spc_id | user_id |
+-----------+---------------------+
| 1 | 1 |
+-----------+---------------------+
| 2 | 1 |
+-----------+---------------------+
| 3 | 1 |
+-----------+---------------------+
| 1 | 2 |
+-----------+---------------------+
and there is a third table with each specialty’s id and name which is not important.
As you can see, each user can have many different specialties. Now in each user profile, I want to show a list of similar users (specialty wise!).
Something like:
$user_speciltys_in_array = $user->get_thisUser_specialtys();
$sql = "select `usrname` from users join user_specialtys " .
"where 'they have most similarity to $user_specialtys_in_array'";
I’m not sure how to do this.
EDIT: Oops! I didn’t read the question carefully! You wanted the usernames! So we need to wrap the query above as a sub-query and use the
user_ids to fine the usernames. (Modified below.)Let’s look for user ID’s by counting the number of matching specialities. We’ll build our SQL query using the
$user_speciltys_in_array:For example, if
$user_speciltys_in_arrayisarray(1,2), you’ll get these results:I added
ORDER BY score DESCandLIMIT 10to just get the 10 best-matching users, which is often what people look for in a recommendation system (a short list of recommendations).