Checking the manufacturer for product availability

Hello, there is a regular loop that outputs the name and id

$res = mysql_query(“SELECT id,name
FROM Manufacturer WHERE Checked=1
ORDER BY name”);

while ($row = mysql_fetch_array($res)) {
printf ("<li><a href='/manufacturer.php?id=%s'>%s</a>",$row['id'],$row['name']);
}

How to make a check that would display those manufacturers where there are goods, and inside the sql loop make a request for the existence of goods? There has been a huge load on the site. Is there a better solution?


Answer 1

SELECT DISTINCT m.id, m.name 
FROM Manufacturer M
   JOIN Product P ON P.manufacturer = M.id
WHERE M.Checked=1 
ORDER BY m.name

names of the products table and its columns are fictitious