Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2010-01-04
2
Medium Priority
?
221 Views
Last Modified: 2013-12-12
Hi,

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 t1.id.

Table 2 structure:

id    t1.id    ext.id

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.

Example:

Checkboxes checked:

Power (ext.id = 2)
Cabel TV (ext.id = 5)
Garage (ext.id = 6)
Garden (ext.id = 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!
0
Comment
Question by:bgpro_no
2 Comments
 
LVL 26

Accepted Solution

by:
tigin44 earned 2000 total points
ID: 26171691
as I understand you are looking for a query like this
SELECT T2.*
FROM Table_1 T1
	INNER JOIN Table_2 T2 ON T1.id = T2.id AND T2.ext.id = 2
	INNER JOIN Table_2 T3 ON T1.id = T3.id AND T2.id = T3.id AND T3.ext.id = 5
	INNER JOIN Table_2 T4 ON T1.id = T4.id AND T2.id = T4.id AND T4.ext.id = 6
	INNER JOIN Table_2 T5 ON T1.id = T5.id AND T2.id = T5.id AND T5.ext.id = 10

Open in new window

0
 

Author Closing Comment

by:bgpro_no
ID: 31672415
Thanks - worked perfectly!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…
Suggested Courses
Course of the Month20 days, 23 hours left to enroll

810 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