我正在进行SQL查询,我希望根据类别将相关产品提供给产品。 我的查询如下:
$sql=
"SELECT * FROM `products` WHERE products.id IN (
SELECT product_categories.product_id FROM `product_categories` WHERE product_categories.category_id=(
SELECT product_categories.category_id FROM `product_categories` WHERE product_categories.product_id=".
$this->db->escape($product_id)."));";
return $this->db->query($sql)->result(static::$model_name);//return related products array
但这也给了我原始产品,我不想要原始产品,我无法使用JOINS这样做,所以我切换到仍然卡住的基本查询。如何在此查询中排除它?
答案 0 :(得分:2)
我认为你可以通过加入来解决这个问题:
$sql = 'SELECT products.*
FROM products
JOIN product_categories ON product_categories.product_id = products.id
WHERE product_categories.product_id != '. $this->db->escape($product_id);
return $this->db->query($sql)->result(static::$model_name);//return related products array