Forum Moderators: coopster
<?php
$dbhost = '****';
$dbuser = '*****';
$dbpass = '******';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
$dbname = '#*$!xx';
mysql_select_db($dbname);
$result = mysql_query("SELECT product_id FROM YOURTABLE
WHERE category_id='11,14,7'");
echo "<table border='1'>
<tr>
<th>Product ID</th>
</tr>";
while ($row = mysql_fetch_assoc($result))
{
echo "<tr>";
echo "<td>" . $row['product_id'] . "</td>";
echo "</tr>";
}
echo "</table>";
$result = mysql_query("SELECT product_id FROM YOURTABLE
WHERE category_id in(11,14,7) order by product_id, category_id");
mysql> select * from tt;
+----+------------+------------------+
| id | product_id | product_category |
+----+------------+------------------+
| 1 | 29 | 11 |
| 2 | 29 | 14 |
| 3 | 29 | 7 |
| 4 | 30 | 11 |
| 5 | 30 | 7 |
| 6 | 32 | 11 |
| 7 | 32 | 14 |
| 8 | 32 | 26 |
| 9 | 32 | 7 |
+----+------------+------------------+
9 rows in set (0.00 sec)
mysql> select t1.id, t1.product_id
from tt as t1
inner join tt as t2
on t1.product_id = t2.product_id
inner join tt as t3
on t1.product_id = t3.product_id
where t1.product_category=11
and t2.product_category=14
and t3.product_category=7;
+----+------------+
| id | product_id |
+----+------------+
| 1 | 29 |
| 6 | 32 |
+----+------------+
2 rows in set (0.00 sec)