2

我有 4 张桌子,这都是有关系的,是的,他们彼此相爱 :p

  • 产品
  • 产品类别
  • 产品类别关系
  • 产品图片

我想在一个查询中加入这 4 个表,这是我的 Codeigniter 代码;

function list_products($limit = 10, $offset = 0, $category)
{
    $this->db->select('p.url, p.name, p.title, i.image');
    $this->db->from('products p');
    $this->db->join('product_categories c','c.id = r.category_id',
                    'left');
    $this->db->join('product_category_relations r',
                    'r.category_id = c.id','left');
    $this->db->join('product_images i',
                    'p.id = i.product_id AND i.default = 1','left');
    $this->db->where('c.url',$this->category_url.$category);
    $this->db->limit($limit,$offset);
    return $this->db->get()->result();
}

当我执行这个函数/查询时,结果是关于定义“r”的错误。

“on 子句”中的未知列“cms_r.category_id”

查询:

选择purl, p. name, p. title, i. image从 ( cms_productsp) 左连接cms_product_categoriesc 开启cid= cms_rcategory_id左加入cms_product_category_relationsr ON rcategory_id= cid左加入cms_product_imagespid= iproduct_idAND i.default = 1 WHERE curl= '类别/yilbasi' 限制 12

你能帮助我吗?

4

2 回答 2

0

您使用后分配r类似于product_category_relations

你在这里写:

$this->db->join('product_categories c','c.id = r.category_id','left');

但什么是r?你只在下一个陈述中回答这个问题。

$this->db->join('product_category_relations r','r.category_id = c.id','left');

如果您想要具体答案,请提供示例 SQL 创建脚本。

于 2012-08-11T14:37:16.700 回答
0

@Edward Ruchevits 由于 r.category_id 在加入 product_category_relations r 后定义,您的查询未加入,您需要productsproduct_categories之间的关系,然后您的查询类似于

$this->db->join('product_categories c','c.product_id = p.id','left');
于 2017-07-20T07:22:15.527 回答