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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:14:58+00:00 2026-06-14T06:14:58+00:00

I have a feature to search for users on my website, I also have

  • 0
  • I have a feature to search for users on my website,
  • I also have a feature for friends on my site.
  • I have a query to search for the correct users on my site, and
  • I have a query that determines the users that a user is friends with, they both work as they should,

so what i need assistance with is combining the two, to order friends before other users.

Search query:

SELECT userID, fname, lname, 
FROM names 
WHERE (
    fname LIKE '".$term."%' 
    OR lname LIKE '".$term."%' 
    OR CONCAT(fname, ' ', lname) LIKE '".$term."%'
      ) 
AND userID!=$session 
ORDER BY fname ASC, lname ASC 
LIMIT 10

Friends query:

SELECT CASE WHEN userID=$session 
            THEN userID2 
            ELSE userID 
            END AS friendID 
FROM friends
WHERE (
       userID=$session 
       OR userID2=$session
       ) 
AND state='1'

so, a statement that would compare the two should be something like, IF names.userID=friends.friendID

Now I found a users suggestion for a similar issue on stackoverflow that seems like it would be a simple way of doing this, however I’m not entirely sure how to add it with my particular friends query, as it’s more of a complex query.

SELECT
    posting.id,
    IF(a.friend_id = b.criteria, 1, 0) AS is_friend
...
ORDER BY is_friend DESC, posting.id DESC
  • 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-14T06:14:59+00:00Added an answer on June 14, 2026 at 6:14 am

    Use an outer join (left join) to join the user list returned by your first query with the friend list that your second query produces. Now you’ll be able to sort on the fact whether the second row set has a match: if it does, the corresponding row should go first.

    This is an example of how you could implement the above:

    SELECT
      n.userID,
      n.fname,
      n.lname
    FROM names n
    LEFT JOIN (
      SELECT CASE WHEN userID=$session 
                  THEN userID2 
                  ELSE userID 
                  END AS friendID 
      FROM friends
      WHERE (
             userID=$session 
             OR userID2=$session
             ) 
      AND state='1'
    ) f
    ON n.userID = f.friendID
    WHERE (
        n.fname LIKE '".$term."%' 
        OR n.lname LIKE '".$term."%' 
        OR CONCAT(n.fname, ' ', n.lname) LIKE '".$term."%'
          ) 
    AND n.userID!=$session 
    ORDER BY (f.friendID IS NULL), n.fname ASC, n.lname ASC 
    LIMIT 10
    

    The first sorting criterion, (f.friendID IS NULL), evaluates to false if there’s a matching f row, i.e. if the user is a friend, and to true otherwise. Consequently, since false sorts before true, the friends will be sorted before non-friends.

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

Sidebar

Related Questions

I'm using jQuery. I have website feature that does an ajax search and returns
I have a group feature on my website that allows 1 user to put
We have a search feature that allows users to search based on product description.
So I have this website that has a search feature which searches a table
We have a web app that has a built-in search feature that our users
In my website I have implemented search feature using full-text search. It works fine
I have created a search feature for a site and in addition I made
I have an app that includes a search feature. This feature is implemented by
I have been tasked with creating a site wide search feature. The search needs
Search is the most used feature on our website and the search query is

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.