INNER JOIN WITH WHERE MYSQL

1

I have a foreach that runs each product, then I would like to do a query on two tables from that ID, but I'm not getting it, what am I doing wrong?

$collection = Mage::getModel('catalog/product')->getCollection();
foreach ($collection as $key => $produto) {
$id = $produto->getId();

$produto_loaded = Mage::getModel('catalog/product')->load($id);

$id_prd = $produto_loaded->getId();  

$teste = $id_prd;
$link = mysqli_connect('localhost','root','','teste2');
$link->set_charset("utf8");
$query =
"SELECT catalog_product_website.website_id as websiteid, catalog_category_product.category_id as categoria, catalog_category_product.position as positioncat
FROM catalog_product_website
INNER JOIN catalog_category_product
ON catalog_product_website.websiteid = catalog_category_product.product_id
WHERE n.websiteid = '$teste'";
$select = mysqli_query($link, $query);
while ($row = mysqli_fetch_array($select)) {
$test = array(
$row['websiteid'],
$row['categoria'],
$row['positioncat']);
print_r($test);
}
    
asked by anonymous 23.11.2017 / 17:56

1 answer

1
SELECT a.website_id as websiteid, 
b.category_id as categoria, 
b.position as positioncat
FROM catalog_product_website a
INNER JOIN catalog_category_product b
ON a.website_id = b.product_id
WHERE a.websiteid = '$teste'";

Test this script

    
23.11.2017 / 18:00