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

  • Home
  • SEARCH
  • 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 1059089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:07:14+00:00 2026-05-16T18:07:14+00:00

Ok, let’s suppose we have members table. There is a field called, let’s say,

  • 0

Ok, let’s suppose we have members table. There is a field called, let’s say, about_member. There will be a string like this 1-1-2-1-2 for everybody. Let’s suppose member_1 has this string 1-1-2-2-1 and he searches who has the similar string or as much similar as possible. For example if member_2 has string 1-1-2-2-1 it will be 100% match, but if member_3 has string like this 2-1-1-2-1 it will be 60% match. And it has to be ordered by match percent. What is the most optimal way to do it with MYSQL and PHP? It’s really hard to explain what I mean, but maybe you got it, if not, ask me. Thanks.

Edit: Please give me ideas without Levenshtein method. That answer will get bounty. Thanks. (bounty will be announced when I will be able to do that)

  • 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-05-16T18:07:15+00:00Added an answer on May 16, 2026 at 6:07 pm

    Jawa posted this idea originally; here is my attempt.

    ^ is the XOR function. It compares 2 binary numbers bit-by-bit and returns 0 if both bits are the same, and 1 otherwise.

        0 1 0 0 0 1 0 1 0 1 1 1  (number 1)
     ^  0 1 1 1 0 1 0 1 1 0 1 1  (number 2)
     =  0 0 1 1 0 0 0 0 1 1 0 0  (result)
    

    How this applies to your problem:

      // In binary...
      1111 ^ 0111 = 1000 // (1 bit out of 4 didn't match: 75% match)
      1111 ^ 0000 = 1111 // (4 bits out of 4 didn't match: 0% match)
    
      // The same examples, except now in decimal...
        15 ^    7 = 8  (1000 in binary) // (1 bit out of 4 didn't match: 75% match)
        15 ^    0 = 15 (1111 in binary) // (4 bits out of 4 didn't match: 0% match)
    

    How we can count these bits in MySQL:

    BIT_COUNT(b'0111') = 3 // Bit count of binary '0111'
    BIT_COUNT(7) = 3       // Bit count of decimal 7 (= 0111 in binary)
    BIT_COUNT(b'1111' ^ b'0111') = 1 // (1 bit out of 4 didn't match: 75% match)
    

    So to get the similarity…

    // First we focus on calculating mismatch.
    (BIT_COUNT(b'1111' ^ b'0111') / YOUR_TOTAL_BITS) = 0.25 (25% mismatch)
    (BIT_COUNT(b'1111' ^ b'1111') / YOUR_TOTAL_BITS) = 0 (0% mismatch; 100% match)
    
    // Now, getting the proportion of matched bits is easy
    1 - (BIT_COUNT(b'1111' ^ b'0111') / YOUR_TOTAL_BITS) = 0.75 (75% match)
    1 - (BIT_COUNT(b'1111' ^ b'1111') / YOUR_TOTAL_BITS) = 1.00 (100% match)
    

    If we could just make your about_member field store data as bits (and be represented by an integer), we could do all of this easily! Instead of 1-2-1-1-1, use 0-1-0-0-0, but without the dashes.

    Here’s how PHP can help us:

    bindec('01000') == 8;
    bindec('00001') == 1;
    decbin(8) == '01000';
    decbin(1) == '00001';
    

    And finally, here’s the implementation:

    // Setting a member's about_member property...
    $about_member = '01100101';
    $about_member_int = bindec($about_member);
    $query = "INSERT INTO members (name,about_member) VALUES ($name,$about_member_int)";
    
    // Getting matches...
    $total_bits = 8; // The maximum length the member_about field can be (8 in this example)
    $my_member_about = '00101100';
    $my_member_about_int = bindec($my_member_about_int);
    $query = "
        SELECT 
            *,
            (1 - (BIT_COUNT(member_about ^ $my_member_about_int) / $total_bits)) match 
        FROM members
        ORDER BY match DESC
        LIMIT 10";
    

    This last query will have selected the 10 members most similar to me!

    Now, to recap, in layman’s terms,

    We use binary because it makes things easier; the binary number is like a long line of light switches. We want to save our “light switch configuration” as well as find members that have the most similar configurations.

    The ^ operator, given 2 light switch configurations, does a comparison for us. The result is again a series of switches; a switch will be ON if the 2 original switches were in different positions, and OFF if they were in the same position.

    BIT_COUNT tells us how many switches are ON–giving us a count of how many switches were different. YOUR_TOTAL_BITS is the total number of switches.

    But binary numbers are still just numbers… and so a string of 1’s and 0’s really just represents a number like 133 or 94. But it’s a lot harder to visualize our “light switch configuration” if we use decimal numbers. That’s where PHP’s decbin and bindec come in.

    Learn more about the binary numeral system.

    Hope this helps!

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

Sidebar

Related Questions

Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I'm building a data access layer for an application. Typically I have
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say I have a drive such as C:\ , and I want to
Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,
Let's say you create a wizard in an HTML form. One button goes back,
Let me try to explain what I need. I have a server that is
Let's say I'm writing a PHP (>= 5.0) class that's meant to be a
Let's aggregate a list of free quality web site design templates. There are a
Let me preface this question by saying I use TextMate on Mac OSX for

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.