加入

时间:2017-08-17 15:17:57

标签: mysql sql

我有以下SQL查询:

SELECT *, oc_category.image AS category_image 
FROM oc_product p 
   LEFT JOIN oc_product_description pd 
      ON (p.product_id = pd.product_id) 
   LEFT JOIN uploaded_codes uc 
      ON p.product_id = uc.product_id 
   INNER JOIN oc_product_to_category ptc 
      ON ptc.product_id = p.product_id 
   INNER JOIN oc_category c 
      ON ptc.category_id = c.category_id 
   INNER JOIN oc_category_description cd 
      ON cd.category_id = c.category_id 
WHERE pd.language_id = '2' 
   AND pd.name LIKE '%ч%' 
GROUP BY p.product_id 
ORDER BY pd.name ASC LIMIT 0,20

产品和类别表中都包含image列,当它返回结果时,类别图像会覆盖产品图像。如何在类别图像上添加别名,以便它可以作为category_image而产品可以作为image

我尝试的以下示例返回错误

  

致命错误:未捕获异常:错误:未知列   ' oc_category.image'在'字段列表

2 个答案:

答案 0 :(得分:1)

首先,您已经为oc_category表提供了别名,您应该使用它。并在列中使用保留字的反引号:

SELECT *, c.`image` AS category_image 
FROM oc_product p 
LEFT JOIN oc_product_description pd 
    ON (p.product_id = pd.product_id) 
LEFT JOIN uploaded_codes uc 
    ON p.product_id = uc.product_id 
INNER JOIN oc_product_to_category ptc 
    ON ptc.product_id = p.product_id 
INNER JOIN oc_category c 
    ON ptc.category_id = c.category_id 
INNER JOIN oc_category_description cd 
    ON cd.category_id = c.category_id 
WHERE pd.language_id = '2' 
AND pd.name LIKE '%ч%' 
GROUP BY p.product_id 
ORDER BY pd.name ASC LIMIT 0,20;

答案 1 :(得分:0)

因为你已经给了别名" c"到表" oc_category",在选择的条款中将oc_category.image更改为c.image