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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:43:45+00:00 2026-06-16T00:43:45+00:00

Say I have the following table, named data : ID foo1 foo2 foo3 1

  • 0

Say I have the following table, named data:

ID   foo1     foo2    foo3
1    11       22      33
2    22       17      92
3    31       33      53
4    53       22      11
5    43       23      9

I want to select all rows where either foo1, foo2 or foo3 match either of these columns in the first row. That is, I want all rows where at least one of the foos appears also in the first row. In the example above, I want to select rows 1, 2, 3 and 4. I thought that I could use something like

SELECT * FROM data WHERE foo1 IN (SELECT foo1,foo2,foo3 FROM data WHERE ID=1)
                      OR foo2 IN (SELECT foo1,foo2,foo3 FROM data WHERE ID=1)
                      OR foo3 IN (SELECT foo1,foo2,foo3 FROM data WHERE ID=1)

but this does not seem to work. I can, of course, use

WHERE foo1=(SELECT foo1 FROM data WHERE ID=1) 
   OR foo1=(SELECT foo2 FROM data WHERE ID=1) 
   OR ...

but that would invlove many lines, and in my real data set there are actually 16 columns, so it will really be a pain in the lower back. Is there a more sophisticated way to do so?

Also, what should I do if I want to count also the number of hits (in the example above, get 4 for row 1, 2 for row 4, and 1 for rows 2,3)?

  • 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-16T00:43:45+00:00Added an answer on June 16, 2026 at 12:43 am
    SELECT data.*,
          (data.foo1 IN (t.foo1, t.foo2, t.foo3))
        + (data.foo2 IN (t.foo1, t.foo2, t.foo3))
        + (data.foo3 IN (t.foo1, t.foo2, t.foo3)) AS number_of_hits
    FROM   data JOIN data t ON t.id = 1
    WHERE  data.foo1 IN (t.foo1, t.foo2, t.foo3)
        OR data.foo2 IN (t.foo1, t.foo2, t.foo3)
        OR data.foo3 IN (t.foo1, t.foo2, t.foo3)
    

    See it on sqlfiddle.

    Actually, on reflection, you might consider normalising your data:

    CREATE TABLE data_new (
      ID         BIGINT  UNSIGNED NOT NULL,
      foo_number TINYINT UNSIGNED NOT NULL,
      val        INT,
      PRIMARY KEY (ID, foo_number),
      INDEX (val)
    );
    
    INSERT INTO data_new
      (ID, foo_number, val)
              SELECT ID, 1, foo1 FROM data
    UNION ALL SELECT ID, 2, foo2 FROM data
    UNION ALL SELECT ID, 3, foo3 FROM data;
    
    DROP TABLE data;
    

    Then you can do:

    SELECT   ID,
             MAX(IF(foo_number=1,val,NULL)) AS foo1,
             MAX(IF(foo_number=2,val,NULL)) AS foo2,
             MAX(IF(foo_number=3,val,NULL)) AS foo3,
             number_of_hits
    FROM     data_new JOIN (
      SELECT   d1.ID, COUNT(*) AS number_of_hits
      FROM     data_new d1 JOIN data_new d2 USING (val)
      WHERE    d2.ID = 1
      GROUP BY d1.ID
    ) t USING (ID)
    GROUP BY ID
    

    See it on sqlfiddle.

    As you can see from the execution plan, this will be considerably more efficient for large data sets.

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

Sidebar

Related Questions

Lets say i have a table with the following data Customer table: Name amount
Let's say I have the following hypothetical data structure: create table country ( country_id
For the question, Let's say I have a table that holds the following data
I have the following mysql table called test. lets say it has the data
Let's say I have following table like this <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0
Let's say I have the following table: Value Time 0 15/06/2012 8:03:43 PM 1
Let's say we have the following table: CREATE TABLE T ( ID INT, String1
The idea is that say you have the following table. ------------- | oID |
Let's say I have the following database table: record_id | record_date | record_value -----------+-------------+--------------
Lets say I have table with following columns 1. Client - string. 2. Profit

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.