我有 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”
查询:
选择
p。url,p.name,p.title,i.image从 (cms_productsp) 左连接cms_product_categoriesc 开启c。id=cms_r。category_id左加入cms_product_category_relationsr ONr。category_id=c。id左加入cms_product_images我p。id=i。product_idAND i.default = 1 WHEREc。url= '类别/yilbasi' 限制 12
你能帮助我吗?