Unknow column ''first_order' in where clause

时间:2018-03-25 21:03:37

标签: mysql sql prestashop

Below I have my SQL query from Prestashop. The error is: Unknown column 'first_order' in 'where clause'.

SELECT SQL_NO_CACHE SQL_CALC_FOUND_ROWS
a.`id_order`, `reference`, `total_paid_tax_incl`, `payment`, a.`date_add` AS `date_add`
, 
a.id_currency,
a.id_order AS id_pdf,
CONCAT(LEFT(c.`firstname`, 1), '. ', c.`lastname`) AS `customer`,
osl.`name` AS `osname`,
os.`color`,
IF((SELECT COUNT(id_customer) FROM `ps_orders` so LEFT JOIN ps_order_state ost ON ost.id_order_state = so.current_state WHERE so.id_customer = a.id_customer AND NOT ost.id_order_state = 6 AND NOT ost.id_order_state = 7 AND NOT ost.id_order_state = 8) = 1, 1, 0) as first_order,
IF((SELECT so.id_order FROM `ps_orders` so WHERE so.id_customer = a.id_customer AND so.id_order < a.id_order LIMIT 1) > 0, 0, 1) as new,
country_lang.name as cname,
IF(a.valid, 1, 0) badge_success
FROM `ps_orders` a 
LEFT JOIN `ps_customer` c ON (c.`id_customer` = a.`id_customer`)
LEFT JOIN `ps_address` address ON address.id_address = a.id_address_delivery
LEFT JOIN `ps_country` country ON address.id_country = country.id_country
LEFT JOIN `ps_country_lang` country_lang ON (country.`id_country` = country_lang.`id_country` AND country_lang.`id_lang` = 6)
LEFT JOIN `ps_order_state` os ON (os.`id_order_state` = a.`current_state`)
LEFT JOIN `ps_order_state_lang` osl ON (os.`id_order_state` = osl.`id_order_state` AND osl.`id_lang` = 6) 
WHERE 1   AND `first_order` = 1 
ORDER BY a.`id_order` DESC  LIMIT 0, 50

I'm working at MySQL 10.1.23-MariaDB. Thanks for any help.

2 个答案:

答案 0 :(得分:1)

You can't access aliases in a WHERE clause (see the manual). You will need to use a HAVING clause instead.

答案 1 :(得分:0)

WHERE 1 AND first_order = 1

First_order is a user defined column, and is not available in context of this filter.