I have the following kind of setup (a bit oversimplified):
An array (php): array('a', 'b', 'c');
Table 1: id
with values (1), (2), (3), (others).
Table 2: id
, idT1
,value
with values (1, 1, 'a'), (2, 1, 'b'), (3, 1, 'c'), (4, 2, 'c'), (5, 77, 'w') (others).
I need to select rows from table 1, that have correspondent in table 2 with ALL the values in the array (in this case the row with id col = 1).
A way to do this would be something like (ignore possible typos pls)
<?php
$query = "SELECT * FROM `table1` WHERE `id` IN ";
$subqr = "";
foreach ($arr as $a) {
if (!$subqr)
$subqr = "(SELECT `idT1` FROM `table2` WHERE `value` = '$a')";
else
$subqr .= " AND `id` IN (SELECT `idT1` FROM `table2` WHERE `value` = '$a')";
}
ExecuteQuery($query.$subqr); // Where ExecuteQuery gets query as param.
Now my problem is that... I'm not exactly comfortable with the solution... I mean... a foreach that adds subqueries is likely not to be the best approach. If the array has 9999 elements... 9999 subqueries would just kill mysql. Also bear in mind that this is oversimplified.
Any other ideas how to do this?