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 8184889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:39:43+00:00 2026-06-07T01:39:43+00:00

I have two tables Schools and Users Table School —————————————————– | id | name

  • 0

I have two tables Schools and Users

Table School

-----------------------------------------------------
| id |     name    |  city  |     major    | userID |
----------------------------------------------------|
| 1  |  school A   | chicago |    CS       |    1   |
----------------------------------------------------|
| 2  |  school B   | chicago |    CS       |    1   |
----------------------------------------------------|
| 3  |  school A   | chicago |    CS       |    2   |
----------------------------------------------------|
| 4  |  school C   | chicago |    Art      |    2   |
----------------------------------------------------|
| 5  |  school B   | chicago |    CS       |    3   |
----------------------------------------------------|
| 6  |  school D   | chicago |    Math     |    3   |
----------------------------------------------------|
| 7  |  school A   |New York |    CS       |    3   |
----------------------------------------------------|
| 8  |  school B   | chicago |    Art      |    3   |
-----------------------------------------------------

Table Users

--------------------
| id |     name    |
____________________
| 1  |    User A   |
____________________
| 2  |    User B   |
____________________
| 3  |    User C   |
____________________
| 4  |    User D   |
____________________
| 5  |    User E   |
____________________

The userID field in the schools table is a foreign key to the id field in the users table. I want to write a MySQL statement that takes a given userID and lists all classmates of that user.

So, for the example above, a class mate of User A (ID#1) is a user that went to the same school as User A, is located in the same city, and has the same major. Thus, valid classmates for User A are only User B (ID#2) AND User C (ID#3).

Right now, I am using two MySQL statements to accomplish this goal. The first one is this

SELECT id FROM schools WHERE userID = '1'

which lists all schools for User A. Then I use PHP to loop through the results and for each row I run the following

    SELECT userID from schools WHERE 
name city LIKE '%$chicago%' 
AND name LIKE '%$school A%' 
AND major LIKE '%$CS%'

This works fine and returns the right list of userIDs. However, I am wondering if there is a more efficient way to do this in one SQL statement and without having to use PHP.

  • 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-06-07T01:39:44+00:00Added an answer on June 7, 2026 at 1:39 am

    You can do it like this:

    select distinct u.name
    from Users u
    join School s on s.userID = u.id
    join (
                select distinct s.name, s.city
                from School s
                inner join Users u on u.id = s.userID
                where u.name = 'User A'
               ) aux on s.name = aux.name and s.city = aux.city
    where u.name <> 'User A'
    

    In the aux query you select the name‘s and city‘s of a @user and then select all users with those requirements and that aren’t the @user itself.

    You may replace ‘User A’ by a variable.

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

Sidebar

Related Questions

Have two tables with a linking table between them. USERS +-------+---------+ | userID| Username|
I have two tables one with ID and NAME table 1 ID | NAME
I have two tables user table user_id | name | 1 | peter |
I have two tables. Table Emp id name 1 Ajay 2 Amol 3 Sanjay
I have two tables that have a many-many relationship: Player(personID, school) Team(teamID, name) What
I have two tables: Person (personID, name, address, phone, email) Player (dateOfBirth, school) What
I have two tables in my MySQL database, users and tweets, as follows: TABLE
I have two tables: table a ida valuea 1 a 2 b 3 c
I have two tables, a user table and a application table: User id username
I have a SQL Server database with two table : Users and Achievements. My

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.