我有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_products
页)左加入cms_product_categories
C 上c
.id
=cms_r
.category_id
左加入cms_product_category_relations
R 打开r
.category_id
=c
.id
左加入cms_product_images
我p
.id
=i
.product_id
和 i.默认值 = 1 其中c
.url
= "卡特戈里/伊尔巴斯"限制 12
你可以帮我吗?
您可以在使用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 创建脚本。
@Edward Ruchevits 您的查询没有加入,因为r.category_id连接后定义 product_category_relations r,您需要 产品之间的关系 product_categories然后你的查询是这样的
$this->db->join('product_categories c','c.product_id = p.id','left');