I know there are a few ways to do this, but I'm having a brain fart and I'm unsure what the simplest SQL would be.
Basically, I have a table of product brands and a table of products. There can be many products to each brand. On my website I only want to display links to brands that actually have products. So I want a simple query that will select the name of each brand whose ID is referenced in the products table.
b.name AS brand_name
FROM brands AS b
JOIN products AS p
ON p.brand = b.id
Perfect. Exactly what I thought, but for some reason I was convinced that was a better way than using DISTINCT. Anyway, does the job nicely, thanks
here's another method
SELECT name AS brand_name
( SELECT NULL
WHERE brand = brands.id )
you might want to test both (and/or do an EXPLAIN on both) to see which one runs better in your environment