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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:58:26+00:00 2026-06-16T17:58:26+00:00

Ok, so im working out a search function for MySQL but have come across

  • 0

Ok, so im working out a search function for MySQL but have come across something I can’t seem to work out. I am trying to make a script to search the users friends, and return the friends with a better score. This is what I have so far:

SELECT id, first_name, last_name, image_id FROM users
WHERE id IN (
    SELECT receiver AS friend FROM friends WHERE initiator = :me AND status = :status        
     UNION
    SELECT initiator AS friend FROM friends WHERE receiver = :me AND status = :status
) ORDER BY score AND concat(first_name, ' ', last_name) LIKE '%test query%';

Now everything works till I try and order the friends by Score. Can anyone workout how to order the friends by score? If you need me to clarify anything just ask.

I have the following two tables.

Users Table: enter image description here

Friends Table:enter image description here

As requested here is some sample data:

As an example, I have three users:

ID: 1, first_name: Michael, last_name: Sample
ID: 2, first_name: John, last_name: Citizen
ID: 3, first_name: Caitlin, last_name: Wired

The first entry below will make Michael and Caitlin be friends by showing that Michael initiated a friend request and Caitlin (the receiver) accepted it. (Hence the status of 1) their base score is 10. The second entry shows that John is also friends with Caitlin (initiated by Caitlin) but have a better score.

ID: 10000, initiator: 1, receiver: 3, status: 1, score: 10
ID: 10001, initiator: 3, receiver: 2, status: 1, score: 20

So in the case of this sample data. If Caitlin was the user searching for her friends, it should show John than Michael.

  • 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-16T17:58:27+00:00Added an answer on June 16, 2026 at 5:58 pm

    The outer statement selects only from the users column, which doesn’t have a score column. Just about any statement using sub-selects can (and should) be re-written using joins. In this particular case, it will allow all the friends fields to be used in the clauses.

    SELECT u.id, u.first_name, u.last_name, u.image_id 
      FROM users AS u
        JOIN friends AS f 
          ON ...
    

    The expressions used in the WHERE clauses of the sub-selects become the join condition. A union is in a sense equivalent to OR (both are basically conjunctions).

          ON    (u.id = f.receiver AND f.initiator=:me AND status=:status)
             OR (u.id = f.initiator AND f.receiver=:me AND status=:status)
    

    The rest of the statement pretty much remains as it is.

      WHERE concat(first_name, ' ', last_name) LIKE '%test query%'
      ORDER BY score
    ;
    

    All together, that’s:

    SELECT u.id, u.first_name, u.last_name, u.image_id 
      FROM users AS u
        JOIN friends AS f 
          ON    (u.id = f.receiver AND f.initiator=:me AND status=:status)
             OR (u.id = f.initiator AND f.receiver=:me AND status=:status)
      WHERE concat(first_name, ' ', last_name) LIKE '%test query%'
      ORDER BY score
    ;
    

    Note that this is untested, and just off the top of my head.

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

Sidebar

Related Questions

I am trying to work out a php function to search the referring page
I'm currently working out the layout of a WPF Application and seem to have
im trying to make some kind of search function in my program which is
I am working with Moodle 2.2.1 but have a basic php-mysql problem. I am
Basically, I have a live search that is working for the two out of
I have to confess that I cannot work out how to complete this. I
I'm working out my first backbone.js app and have run into a bit of
I am working on a function that searches a templated binary search tree for
I just found out that my script is working fine in Chrome, but not
I currently have a PHP file which will search my MySQL database and see

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.