Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7090079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:00:03+00:00 2026-05-28T08:00:03+00:00

I have two tables in my database users table : +———–+———————+———+———+ | id |

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T08:00:04+00:00Added an answer on May 28, 2026 at 8:00 am

    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:

    $user_speciltys_in_array = $user->get_thisUser_specialtys();
    
    $sql = 'SELECT users.user_id, score, username FROM (' .
              'SELECT user_id, COUNT(user_id) AS score FROM user_speciality';
    $first = 1;
    foreach ($user_speciltys_in_array as $spc_id)
    {
       $sql = $sql . (($first==1) ? ' WHERE ' : ' AND ';
       $first = 0;
       $sql = $sql . "(user_speciality.spc_id=$spc_id)";
    }
    $sql = $sql . ' GROUP BY user_id ORDER BY score DESC LIMIT 10) userscores ';
    $sql = $sql . ' LEFT JOIN users ON userscores.user_id = users.user_id';
    

    For example, if $user_speciltys_in_array is array(1,2), you’ll get these results:

    user_id  score  username
    -------  -----  --------
          1      2  max
          2      1  alex
          3      1  thirdguy
    

    I added ORDER BY score DESC and LIMIT 10 to just get the 10 best-matching users, which is often what people look for in a recommendation system (a short list of recommendations).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables in database: comments table users table | u_id | text
I have two tables in my MySQL database, users and tweets, as follows: TABLE
I have a simple database with two tables. Users and Configurations. A user has
I have a table, users, in an Oracle 9.2.0.6 database. Two of the fields
I have two tables in my database: NEWS table with columns: id - the
I have two simple tables in my database. A card table that contains Id,
i have two tables in my database : Users : contain user information UserTypes
I have a database with two tables, Users and Posts. I'd like to write
I have two tables users registered_members I want to confirm values from user table
I have two tables in my MySQL database (table1 and table 2). I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.