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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:30:24+00:00 2026-06-11T12:30:24+00:00

I have 2 tables members ======= id f_name // get these values. l_name email

  • 0

I have 2 tables

members
=======
id
f_name  // get these values.
l_name
email

friends
=======
to
from

The users’s I’d is the value $member_id, if it is present in “to” or “from” I want it to not return the other value of that row, so only non-friends are shown.

Im creating a page to allow members to search through the database to add their friends by email, or name.

I’d like to return all rows from members where there is no record of the userid in either the “to” or “from” columns of the friends table.

I know how to do most basic mysql but joins are an issue for me, i don’t really understand how to write them, if anyone can help me out and maybe even explain it a bit that would be great!

My query now:
  $query = "SELECT * FROM members WHERE f_name LIKE '%" . $word . "%' OR l_name LIKE '%" . $word . "%' OR mc_name LIKE '%" . $word . "%' OR email LIKE '%" . $word . "%' LIMIT 10";
//Where $word is the search term.

Modified Query

SELECT * FROM members WHERE id NOT IN (SELECT to FROM friends WHERE frm=$member_id) AND $member_id NOT IN (SELECT frm FROM friends WHERE to=$member_id) AND f_name LIKE '%" . $word . "%' OR l_name LIKE '%" . $word . "%' OR mc_name LIKE '%" . $word . "%' OR email LIKE '%" . $word . "%' LIMIT 10
  • 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-11T12:30:26+00:00Added an answer on June 11, 2026 at 12:30 pm

    Edited

    So, suppose a user with id 3:

    SELECT * 
    FROM members
    WHERE id <> 3 
    AND id NOT IN (SELECT to FROM friends WHERE from = 3)
    AND id NOT IN (SELECT from FROM friends WHERE to = 3)
    [Other conditions....];
    

    OR

    SELECT * 
    FROM members
    LEFT JOIN friends
    ON (members.id = friends.to
        OR members.id = friends.from)
    AND (members.to = 3 
         OR members.from = 3)
    WHERE friends.to IS NULL
    AND id <> 3
    [Other conditions....];
    

    Maybe consider change the name of your “from” and “to” columns, to avoid confusion, because FROM and TO are reserved words.

    Also, you can use the reserved word EXPLAIN , before the query, to see the difference of the number of rows being fetched.

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

Sidebar

Related Questions

I have two tables users registered_members I want to confirm values from user table
I have 3 tables friends posts members friends ======== id, to, from (user_id's), status
I have the following tables: team_members userid teamid users uid name_f name_l friends friend_id
I have a 2 tables called 'members' and 'users' that both have 2 columns:
Possible Duplicate: MySQL delete row from multiple tables I have 5 tables: members member_videos
I have 2 tables, CLIENTS and MEMBERS . We are trying to get the
I have the following tables: CREATE TABLE members ( member_id int(1) unsigned NOT NULL
I have five tables in my database. Members, items, comments, votes and countries. I
The case: I have 2 tables, 'contracts' and 'members' tied with contract.id = members.cid.
I have the following 3 tables: team_data id team_id team_name team_members userid teamid users

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.