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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:38:48+00:00 2026-06-01T18:38:48+00:00

I have a Table which looks like this: ————————— |housing_id | facility_id | —————————

  • 0

I have a Table which looks like this:

---------------------------
|housing_id | facility_id |
---------------------------
|    1      |      7      |
|    1      |      4      |
|    2      |      7      |
---------------------------

Now what i want to do is get all housing_ids with a facility_id of 7 AND 4.
So the query should only return the housing_id 1 in this case.
Database is mysql.

  • 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-01T18:38:49+00:00Added an answer on June 1, 2026 at 6:38 pm

    Another approach would be –

    SELECT housing_id
    FROM mytable
    WHERE facility_id IN (4,7)
    GROUP BY housing_id
    HAVING COUNT(DISTINCT facility_id) = 2
    

    UPDATE – inspired by the comment by Josvic I decided to do some more testing and thought I would include my findings.

    One of the benefits of using this query is that it is easy to modify to include more facility_ids. If you want to find all housing_ids that have facility_ids 1, 3, 4 & 7 you just do –

    SELECT housing_id
    FROM mytable
    WHERE facility_id IN (1,3,4,7)
    GROUP BY housing_id
    HAVING COUNT(DISTINCT facility_id) = 4
    

    The performance of all three of these queries varies hugely based on the indexing strategy employed. I was unable to get reasonable performance, on my test dataset, from the dependant subquery version regardless of indexing used.

    The self join solution provided by Tim performs very well given separate single column indices on the two columns but does not perform quite so well as the number of criteria increases.

    Here are some basic stats on my test table – 500k rows – 147963 housing_ids with potential values for facility_id between 1 and 9.

    Here are the indices used for running all these tests –

    SHOW INDEXES FROM mytable;
    +---------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+
    | Table   | Non_unique | Key_name            | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type |
    +---------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+
    | mytable |          0 | UQ_housing_facility |            1 | housing_id  | A         |      500537 |     NULL | NULL   |      | BTREE      |
    | mytable |          0 | UQ_housing_facility |            2 | facility_id | A         |      500537 |     NULL | NULL   |      | BTREE      |
    | mytable |          0 | UQ_facility_housing |            1 | facility_id | A         |          12 |     NULL | NULL   |      | BTREE      |
    | mytable |          0 | UQ_facility_housing |            2 | housing_id  | A         |      500537 |     NULL | NULL   |      | BTREE      |
    | mytable |          1 | IX_housing          |            1 | housing_id  | A         |      500537 |     NULL | NULL   |      | BTREE      |
    | mytable |          1 | IX_facility         |            1 | facility_id | A         |          12 |     NULL | NULL   |      | BTREE      |
    +---------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+
    

    First query tested is the dependant subquery –

    SELECT SQL_NO_CACHE DISTINCT housing_id
    FROM mytable
    WHERE housing_id IN (SELECT housing_id FROM mytable WHERE facility_id=4)
    AND housing_id IN (SELECT housing_id FROM mytable WHERE facility_id=7);
    
    17321 rows in set (9.15 sec)
    
    +----+--------------------+---------+-----------------+----------------------------------------------------------------+---------------------+---------+------------+--------+---------------------------------------+
    | id | select_type        | table   | type            | possible_keys                                                  | key                 | key_len | ref        | rows   | Extra                                 |
    +----+--------------------+---------+-----------------+----------------------------------------------------------------+---------------------+---------+------------+--------+---------------------------------------+
    |  1 | PRIMARY            | mytable | range           | NULL                                                           | IX_housing          | 4       | NULL       | 500538 | Using where; Using index for group-by |
    |  3 | DEPENDENT SUBQUERY | mytable | unique_subquery | UQ_housing_facility,UQ_facility_housing,IX_housing,IX_facility | UQ_housing_facility | 8       | func,const |      1 | Using index; Using where              |
    |  2 | DEPENDENT SUBQUERY | mytable | unique_subquery | UQ_housing_facility,UQ_facility_housing,IX_housing,IX_facility | UQ_housing_facility | 8       | func,const |      1 | Using index; Using where              |
    +----+--------------------+---------+-----------------+----------------------------------------------------------------+---------------------+---------+------------+--------+---------------------------------------+
    
    SELECT SQL_NO_CACHE DISTINCT housing_id
    FROM mytable
    WHERE housing_id IN (SELECT housing_id FROM mytable WHERE facility_id=1)
    AND housing_id IN (SELECT housing_id FROM mytable WHERE facility_id=3)
    AND housing_id IN (SELECT housing_id FROM mytable WHERE facility_id=4)
    AND housing_id IN (SELECT housing_id FROM mytable WHERE facility_id=7);
    
    567 rows in set (9.30 sec)
    
    +----+--------------------+---------+-----------------+----------------------------------------------------------------+---------------------+---------+------------+--------+---------------------------------------+
    | id | select_type        | table   | type            | possible_keys                                                  | key                 | key_len | ref        | rows   | Extra                                 |
    +----+--------------------+---------+-----------------+----------------------------------------------------------------+---------------------+---------+------------+--------+---------------------------------------+
    |  1 | PRIMARY            | mytable | range           | NULL                                                           | IX_housing          | 4       | NULL       | 500538 | Using where; Using index for group-by |
    |  5 | DEPENDENT SUBQUERY | mytable | unique_subquery | UQ_housing_facility,UQ_facility_housing,IX_housing,IX_facility | UQ_housing_facility | 8       | func,const |      1 | Using index; Using where              |
    |  4 | DEPENDENT SUBQUERY | mytable | unique_subquery | UQ_housing_facility,UQ_facility_housing,IX_housing,IX_facility | UQ_housing_facility | 8       | func,const |      1 | Using index; Using where              |
    |  3 | DEPENDENT SUBQUERY | mytable | unique_subquery | UQ_housing_facility,UQ_facility_housing,IX_housing,IX_facility | UQ_housing_facility | 8       | func,const |      1 | Using index; Using where              |
    |  2 | DEPENDENT SUBQUERY | mytable | unique_subquery | UQ_housing_facility,UQ_facility_housing,IX_housing,IX_facility | UQ_housing_facility | 8       | func,const |      1 | Using index; Using where              |
    +----+--------------------+---------+-----------------+----------------------------------------------------------------+---------------------+---------+------------+--------+---------------------------------------+
    

    Next is my version using the GROUP BY … HAVING COUNT …

    SELECT SQL_NO_CACHE housing_id
    FROM mytable
    WHERE facility_id IN (4,7)
    GROUP BY housing_id
    HAVING COUNT(DISTINCT facility_id) = 2;
    
    17321 rows in set (0.79 sec)
    
    +----+-------------+---------+-------+---------------------------------+-------------+---------+------+--------+------------------------------------------+
    | id | select_type | table   | type  | possible_keys                   | key         | key_len | ref  | rows   | Extra                                    |
    +----+-------------+---------+-------+---------------------------------+-------------+---------+------+--------+------------------------------------------+
    |  1 | SIMPLE      | mytable | range | UQ_facility_housing,IX_facility | IX_facility | 4       | NULL | 198646 | Using where; Using index; Using filesort |
    +----+-------------+---------+-------+---------------------------------+-------------+---------+------+--------+------------------------------------------+
    
    SELECT SQL_NO_CACHE housing_id
    FROM mytable
    WHERE facility_id IN (1,3,4,7)
    GROUP BY housing_id
    HAVING COUNT(DISTINCT facility_id) = 4;
    
    567 rows in set (1.25 sec)
    
    +----+-------------+---------+-------+---------------------------------+-------------+---------+------+--------+------------------------------------------+
    | id | select_type | table   | type  | possible_keys                   | key         | key_len | ref  | rows   | Extra                                    |
    +----+-------------+---------+-------+---------------------------------+-------------+---------+------+--------+------------------------------------------+
    |  1 | SIMPLE      | mytable | range | UQ_facility_housing,IX_facility | IX_facility | 4       | NULL | 407160 | Using where; Using index; Using filesort |
    +----+-------------+---------+-------+---------------------------------+-------------+---------+------+--------+------------------------------------------+
    

    And last but not least the self join –

    SELECT SQL_NO_CACHE a.housing_id
    FROM mytable a
    INNER JOIN mytable b
        ON a.housing_id = b.housing_id
    WHERE a.facility_id = 4 AND b.facility_id = 7;
    
    17321 rows in set (1.37 sec)
    
    +----+-------------+-------+--------+----------------------------------------------------------------+---------------------+---------+-------------------------+-------+-------------+
    | id | select_type | table | type   | possible_keys                                                  | key                 | key_len | ref                     | rows  | Extra       |
    +----+-------------+-------+--------+----------------------------------------------------------------+---------------------+---------+-------------------------+-------+-------------+
    |  1 | SIMPLE      | b     | ref    | UQ_housing_facility,UQ_facility_housing,IX_housing,IX_facility | IX_facility         | 4       | const                   | 94598 | Using index |
    |  1 | SIMPLE      | a     | eq_ref | UQ_housing_facility,UQ_facility_housing,IX_housing,IX_facility | UQ_housing_facility | 8       | test.b.housing_id,const |     1 | Using index |
    +----+-------------+-------+--------+----------------------------------------------------------------+---------------------+---------+-------------------------+-------+-------------+
    
    SELECT SQL_NO_CACHE a.housing_id
    FROM mytable a
    INNER JOIN mytable b
        ON a.housing_id = b.housing_id
    INNER JOIN mytable c
        ON a.housing_id = c.housing_id
    INNER JOIN mytable d
        ON a.housing_id = d.housing_id
    WHERE a.facility_id = 1
    AND b.facility_id = 3
    AND c.facility_id = 4
    AND d.facility_id = 7;
    
    567 rows in set (1.64 sec)
    
    +----+-------------+-------+--------+----------------------------------------------------------------+---------------------+---------+-------------------------+-------+--------------------------+
    | id | select_type | table | type   | possible_keys                                                  | key                 | key_len | ref                     | rows  | Extra                    |
    +----+-------------+-------+--------+----------------------------------------------------------------+---------------------+---------+-------------------------+-------+--------------------------+
    |  1 | SIMPLE      | b     | ref    | UQ_housing_facility,UQ_facility_housing,IX_housing,IX_facility | IX_facility         | 4       | const                   | 93782 | Using index              |
    |  1 | SIMPLE      | d     | eq_ref | UQ_housing_facility,UQ_facility_housing,IX_housing,IX_facility | UQ_housing_facility | 8       | test.b.housing_id,const |     1 | Using index              |
    |  1 | SIMPLE      | c     | eq_ref | UQ_housing_facility,UQ_facility_housing,IX_housing,IX_facility | UQ_housing_facility | 8       | test.b.housing_id,const |     1 | Using index              |
    |  1 | SIMPLE      | a     | eq_ref | UQ_housing_facility,UQ_facility_housing,IX_housing,IX_facility | UQ_housing_facility | 8       | test.d.housing_id,const |     1 | Using where; Using index |
    +----+-------------+-------+--------+----------------------------------------------------------------+---------------------+---------+-------------------------+-------+--------------------------+
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have table one which looks like this. And I want to get data
I have a table which looks like this: <table id='t'> <thead> .... </thead> <tbody
I have a table which looks like this one: | id | fk_book |
I have this mysql table called comments which looks like this: commentID parentID type
I have two tables which looks something like this Table Queue int ID; string
I have jQuery code which looks something like this on Button1 Click $('table.result_grid tbody
I have a StaffLookup table which looks like this. UserSrn | UserName | ManagerSrn
I have a table in SQL Server which looks like this: ID Code Name
I have table data which looks like this id | keyword | count |
I have a single column table which looks like this: Gaming | Austria |

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.