简体   繁体   中英

Column 'available_for_order' in where clause is ambiguous

I'm strugling with this query within prestashop , i know it could be easily fixed by changing to sa.available_for_order but this way it breaks the logic of other core files , is there any other way around to fix this without renaming available_for_order to sa.available_for_order ,

SELECT
  a.`id_product`,
  b.`name` AS `name`,
  `reference`,
  a.`price` AS `price`,
  sa.`active` AS `active`,
  `newfield`,
  shop.`name` AS `shopname`,
  a.`id_shop_default`,
  image_shop.`id_image` AS `id_image`,
  cl.`name` AS `name_category`,
  sa.`price`,
  0 AS `price_final`,
  a.`is_virtual`,
  pd.`nb_downloadable`,
  sav.`quantity` AS `sav_quantity`,
  sa.`active`,
  IF(sav.`quantity` <= 0, 1, 0) AS `badge_danger`,
  sa.`available_for_order` AS `available_for_order`
FROM
  `ps_product` a
LEFT JOIN
  `ps_product_lang` b
ON
  (
    b.`id_product` = a.`id_product` AND b.`id_lang` = 1 AND b.`id_shop` = 1
  )
LEFT JOIN
  `ps_stock_available` sav
ON
  (
    sav.`id_product` = a.`id_product` AND sav.`id_product_attribute` = 0 AND sav.id_shop = 1 AND sav.id_shop_group = 0
  )
JOIN
  `ps_product_shop` sa
ON
  (
    a.`id_product` = sa.`id_product` AND sa.id_shop = a.id_shop_default
  )
LEFT JOIN
  `ps_category_lang` cl
ON
  (
    sa.`id_category_default` = cl.`id_category` AND b.`id_lang` = cl.`id_lang` AND cl.id_shop = a.id_shop_default
  )
LEFT JOIN
  `ps_shop` shop
ON
  (
    shop.id_shop = a.id_shop_default
  )
LEFT JOIN
  `ps_image_shop` image_shop
ON
  (
    image_shop.`id_product` = a.`id_product` AND image_shop.`cover` = 1 AND image_shop.id_shop = a.id_shop_default
  )
LEFT JOIN
  `ps_image` i
ON
  (
    i.`id_image` = image_shop.`id_image`
  )
LEFT JOIN
  `ps_product_download` pd
ON
  (pd.`id_product` = a.`id_product`)
WHERE
  1 AND `available_for_order` = 1
ORDER BY
  a.`id_product` ASC
LIMIT 0, 50

PRESTASHOP

        public function __construct()
        {
            parent::__construct();
    $this->_select .= ',sa.`available_for_order` AS `available_for_order`, ';
             $this->fields_list['sa!available_for_order'] = array(
                'title' => $this->l('Available for order'),
                'width' => 90,
                'active' => 'available_for_order',
                'filter_key' => 'sa!available_for_order',
                'type' => 'bool',
                'align' => 'center',
                'orderby' => false
            );
}

The column available_for_order must be in more than one of the tables. Just qualify the column name, as you do in the select :

WHERE 1 AND sa.available_for_order = 1
------------^

Modify your fields list to use having filter.

$this->fields_list['available_for_order'] = array(
            'title' => $this->l('Available for order'),
            'width' => 90,
            'active' => 'available_for_order',
            'filter_key' => 'available_for_order',
            'havingFilter' => true,
            'type' => 'bool',
            'align' => 'center',
            'orderby' => false
        );

This will use HAVING instead of WHERE in query.

WHERE clause does not work on column aliases but HAVING does.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM