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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:04:44+00:00 2026-06-02T18:04:44+00:00

I’m making a custom search to a DB that has 6 different columns. To

  • 0

I’m making a custom search to a DB that has 6 different columns.
To make the search I have 6 different input fields that accept only numbers.

I already got the 6 columns search working, the code:

SELECT * FROM data 
WHERE 1_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a')
AND 2_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a')
AND 3_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a')
AND 4_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a')
AND 5_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a')
AND 6_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a')
ORDER BY id ASC;

This gives the correct result despite the order entered.

But I need the select to give me results whether it finds match to 4 or 5 of the numbers entered. I tried the OR but I don’t know how to define the amount of columns matched.

Is this possible?

Thanks for the attention!

edit: ——————-

I got stuck with the code provided by dbaseman.

Because I get a true result but I really don’t know whether it was generated by matching 4, 5 or 6 columns.

I looked to insert a CASE WHEN after, but isn’t working.

Is there a way to know which combination of columns gave me the result?

Thought of something like (rough) if = 4, then result-4, else if = 5, then result-5…etc…
And then be able to put the row in a array.

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-02T18:04:45+00:00Added an answer on June 2, 2026 at 6:04 pm

    Instead of using and/or, sum up the hits:

    SELECT *,
        CASE WHEN 1_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END AS match_1,
        CASE WHEN 2_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END AS match_2,
        CASE WHEN 3_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END AS match_3,
        CASE WHEN 4_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END AS match_4,
        CASE WHEN 5_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END AS match_5,
        CASE WHEN 6_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END AS match_6,
    
        CASE WHEN 1_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END
        + CASE WHEN 2_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END
        + CASE WHEN 3_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END
        + CASE WHEN 4_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END
        + CASE WHEN 5_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END
        + CASE WHEN 6_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END
        AS no_of_matches
    FROM data 
    WHERE 
        (
        CASE WHEN 1_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END
        + CASE WHEN 2_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END
        + CASE WHEN 3_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END
        + CASE WHEN 4_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END
        + CASE WHEN 5_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END
        + CASE WHEN 6_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END
        ) in (4,5,6)
    ORDER BY id ASC;
    

    Alternate, a bit prettier using a nested query:

    SELECT a.*, a.match_1+a.match_2+a.match_3+a.match_4+a.match_5+a.match_6 AS no_of_matches
    FROM (
        SELECT *,
            CASE WHEN 1_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END AS match_1,
            CASE WHEN 2_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END AS match_2,
            CASE WHEN 3_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END AS match_3,
            CASE WHEN 4_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END AS match_4,
            CASE WHEN 5_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END AS match_5,
            CASE WHEN 6_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END AS match_6
            FROM data 
        ) a
    WHERE a.match_1+a.match_2+a.match_3+a.match_4+a.match_5+a.match_6 in (4,5,6)
    ORDER BY id ASC;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has

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.