我想从table1随机获得产品,但我也想检查该产品的帐户公司是否已激活[关闭]

I have two tables products and company

I want to select 6 different products randomly from 'Products table' but I also want to check weather company of this product is activated or not. if its not activated my command must select some other product.

database Sql Language: Php

thanks for help

Try this :

SELECT products.* FROM products p
INNER JOIN companies c 
ON p.company_id = c.id AND c.active = 1
ORDER BY RAND() LIMIT 6;