I have to tables.
The first one (members) contains my customers id|name|email|key
The second (types) contains listings customer subscribes to id|customer_id|type|active
What I would like to do is to list all members that subscribed to a type of list. I can do this with 2 sql:s, but I guess there must be a better and faster way using som kind if JOIN maby and besides I get the wrong ORDER for my customers doing my way. I want ORDER by name.
<?
$type ='555';
$sql = mysql_query(" SELECT * FROM types WHERE type='$type' && active='1' ");
while($a = mysql_fetch_array($sql))
{
$sql2 = mysql_query(" SELECT * FROM members WHERE id='{$a['customer_id']}' ");
while($b = mysql_fetch_array($sql2))
{
echo 'Name: '.$b['name'].'<br>';
}
}
?>
mysql_query(" SELECT * FROM types INNER JOIN members ON types.customer_id = members.id WHERE type='$type' AND active='1' ORDER by members.name ASC");
This should do the trick for you
SELECT * FROM members
JOIN types ON members.id = types.customer_id
WHERE types.type = ? AND types.active = '1'
ORDER BY members.name
perhaps this:
SELECT m.name, m.email
FROM members m left join types t
ON m.id=t.customer_id
WHERE t.type='$type' and t.active='1';