Forum Moderators: coopster
I want the query to display something like this:
Brand 1...Type 1
Brand 1...Type 2
Brand 2...Type 1
Brand 2...Type 2
Brand 3...Type 1
Brand 4...Type 1
Brand 5...Type 1
Brand 5...Type 2
There are approx. 10 Brands and 2 Types in my database. However, not every brand is associated with both types. If not type is associated with the brand, I do not want to display it. Thanks!
<?
$sql = "SELECT t.type, n.name, b.brand FROM cart_number n, cart_type t, cart_brand b WHERE n.type_id=t.type_id AND b.brand_id=n.brand_id ORDER BY brand, type, name";
$result = mysql_query($sql, $db);
while($row = mysql_fetch_assoc($result)) {
?>
<tr>
<td width="50%" align="center" class="content_text"><? echo stripslashes($row['brand']);?></td>
<td width="50%" align="center" class="content_text"><? echo stripslashes($row['type']);?></td>
</tr>
<? }
mysql_free_result($result);
?>
$sql = "SELECT DISTINCT t.type, n.name, b.brand FROM cart_number n, cart_type t, cart_brand b WHERE n.type_id=t.type_id AND b.brand_id=n.brand_id ORDER BY brand, type, name";
This should do.
Another option is keyword GROUP BY
$sql = "SELECT t.type, n.name, b.brand FROM cart_number n, cart_type t, cart_brand b WHERE n.type_id=t.type_id AND b.brand_id=n.brand_id GROUP BY brand, type, name";
Hope this helped you somehow
Best regards!
Michal Cibor
And even the mirrors shall be on our side