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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:46:42+00:00 2026-06-17T17:46:42+00:00

I have tables below: user +———————————————–+ | user_id | username | Password | …

  • 0

I have tables below:

user
+-----------------------------------------------+
|  user_id    |  username   | Password | ...    |
+-----------------------------------------------+
|     1       |     a       |  ***     | ...    |
+-----------------------------------------------+
|     2       |     b       |  ***     | ...    |
+-----------------------------------------------+
|     3       |     c       |  ***     | ...    |
+-----------------------------------------------+
|     4       |     d       |  ***     | ...    |
+-----------------------------------------------+
|     5       |     e       |  ***     | ...    |
+-----------------------------------------------+

friends
+-----------------------------------------------+
|  f_id    |  user_id   | friend_id | ...       |
+-----------------------------------------------+
|     1    |     4      |  2        | ...       |
+-----------------------------------------------+
|     2    |     4      |  1        | ...       |
+-----------------------------------------------+
|     3    |     4      |  5        | ...       |
+-----------------------------------------------+
|     4    |     4      |  3        | ...       |
+-----------------------------------------------+

I want to get all users that are available to be added as friends (in this case, the user_id of 1 will have 3 more friends to be added (2, 3, 5). However, by using the following SQL statement below I only get 1 user (4) available to be added:

$sql = "SELECT * FROM user WHERE user.user_id NOT IN 
(SELECT friends.friend_id FROM friends) AND 
user.user_id <> $_SESSION['id']." ORDER BY RAND() LIMIT 5";

But this works great when I logged in as user 4 which there are no users available to be added. This is a bit tricky to me. Any idea would be very much appreciated.

Thanks

  • 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-17T17:46:43+00:00Added an answer on June 17, 2026 at 5:46 pm

    Edit:

    How about this:
    (in else clause use an invalid id or the same id as the logged in user)

    select * from users where id not in
    (
       select (
                case
                  when uid = 1 then id
                  when fid = 1 then uid
                 else 0
                end
              ) from friends where uid = 1 or fid = 1
    ) and id != 1 order by rand() limit 5;
    

    http://sqlfiddle.com/#!2/12de1/62


    One more way to solve this (but might not be an optimal solution):

    Union query below might not be a costly query compared to complete join between two tables if number of friends an user has is less than total number of users in system.

    Also don’t forget to add index on uid and fid column of friends table.

    select * from users where id not in 
    (
         select id from friends where uid = 1
         union
         select uid from friends where fid = 1
    ) and id != 1 order by rand() limit 5;
    

    http://sqlfiddle.com/#!2/12de1/40

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

Sidebar

Related Questions

I have two tables as below. user_id | username | first_name | role_type -----------------------------------------------------------
I have a number of database tables below. I want to display each user
I have three tables specifying important columns below Users(Id, username) Groups(Id, groupname, creator) (creator
I have three tables (admin,domain,user) as shown below 'admin'(admin_id, email, domain_id, pass) 'domain'(domain_id, name)
I have the two tables below. The table jobs will have a user id
I have 2 tables with values like below: tbl_users user_ID name 1 somename1 2
I have two tables named user and submission. I want to natural join them.
I have four tables in mysql as below: What I want to do is
I have two tables. Table user contains user_id, user_name. Table Transactions contains user_id, transactions
I have tables as below. Table Messages message_id parent_id forum_id user_id Table Users user_id

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.