我有4個表,這一切都在一個關係,是他們彼此相愛:對加入4代表與MySQL和笨
- 產品
- 產品分類
- 產品類別關係
- 產品圖片
我想在一個查詢中加入這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」的錯誤。
未知列在 '關於條款'
查詢 'cms_r.category_id':
選擇
p
。url
,p
。name
,p
。title
,i
。image
FROM (cms_products
p)LEFT JOINcms_product_categories
c ONc
。id
=cms_r
。category_id
LEFT JOINcms_product_category_relations
r ONr
。category_id
=c
。id
LEFT JOINcms_product_images
i ONp
。id
=i
。product_id
AND i.default = 1 WHEREc
。url
= '駕駛員學校/ yilbasi' LIMIT 12
你能幫助我嗎?
R是product_category_relations 。正如你所看到的,我沒有使用「AS」。 – 2012-08-11 14:47:18
@ R.CanserYanbakan但是你使用那個'R'後,你說'R'是'product_category_relations' **。 – 2012-08-11 14:56:21
我知道它應該是什麼,我在問怎麼回事......那麼,我們怎麼能做到這一點? :) – 2012-08-11 15:05:12