Mysql - query where ALL search criterias has to be found in second table

Posted on 2010-01-04
Last Modified: 2013-12-12

I im building a php/mysql based property search.

Tabel one (t1) contains all fields with just one value (title, address etc.). Table two (t2) keeps multiple records for each

Table 2 structure:


In the searchform there are checkboxes for all possible ext.ids and I need to only return results where ALL checked values (ext.ids) is found in t2.


Checkboxes checked:

Power ( = 2)
Cabel TV ( = 5)
Garage ( = 6)
Garden ( = 10)

Now I need to pick up the records in t1 where all these ext.ids are found in t2.

Any suggestions on how to build a mysql query for this?

Thanks in advance!
Question by:bgpro_no
    LVL 26

    Accepted Solution

    as I understand you are looking for a query like this
    SELECT T2.*
    FROM Table_1 T1
    	INNER JOIN Table_2 T2 ON = AND = 2
    	INNER JOIN Table_2 T3 ON = AND = AND = 5
    	INNER JOIN Table_2 T4 ON = AND = AND = 6
    	INNER JOIN Table_2 T5 ON = AND = AND = 10

    Open in new window


    Author Closing Comment

    Thanks - worked perfectly!

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Suggested Solutions

    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to dynamically set the form action using jQuery.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now