嵌套查询导致1064 MySQL

时间:2017-05-14 09:38:51

标签: mysql sql nested

我收到的错误是:

  

1064 - 您的SQL语法出错;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   靠近')作为final_price,(p.products_price * m.nz_manufacturers_value /   c.value + p.p'在第1行

我在phpMyAdmin中运行此查询:

select p.products_quantity, p.products_id, p.manufacturers_id,
        m.manufacturers_currency,p.products_price as products_price_old, p.products_shipping, p.products_tax_class_id, 
        IF(s.status, s.specials_new_products_price, NULL) as specials_new_products_price, 
        IF(s.status, s.specials_new_products_price, p.products_price) as old_final_price,
        (IF(s.status, s.specials_new_products_price, p.products_price) * m.nz_manufacturers_value / c.value + p.products_shipping * 1.4) * (select value from currencies where currencies_id = 5) as final_price,
        (p.products_price * m.nz_manufacturers_value / c.value + p.products_shipping * 1.4) * (select value from currencies where currencies_id = 5) as products_price 
        from products_description pd, products p 
        left join manufacturers m on p.manufacturers_id = m.manufacturers_id 
        left join currencies c on c.code = m.manufacturers_currency 
        left join specials s on p.products_id = s.products_id, products_to_categories p2c 
        where p.products_status = '1' and p.products_id = p2c.products_id and pd.products_id = p2c.products_id and pd.language_id = '1' and p2c.categories_id = '2716'

它工作正常。来自PHP它失败了。

在网站上,我删除了

select value from currencies where currencies_id = 5

(两个实例)和PHP工作正常。

所以PHP并不喜欢这样的嵌套select语句。

是否有一个简单的解决方法,或者我是否需要为该值编写单独的查询,然后将其作为变量应用于查询?

由于

1 个答案:

答案 0 :(得分:0)

您需要额外的括号来包装查询(以及IF语句)并将AS应用于结果,请尝试以下操作:

select p.products_quantity, p.products_id, p.manufacturers_id,
        m.manufacturers_currency,p.products_price as products_price_old, p.products_shipping, p.products_tax_class_id, 
        IF(s.status, s.specials_new_products_price, NULL) as specials_new_products_price, 
        IF(s.status, s.specials_new_products_price, p.products_price) as old_final_price,
        ((IF(s.status, s.specials_new_products_price, p.products_price) * m.nz_manufacturers_value / c.value + p.products_shipping * 1.4) * (select value from currencies where currencies_id = 5)) as final_price,
        (p.products_price * m.nz_manufacturers_value / c.value + p.products_shipping * 1.4) * (select value from currencies where currencies_id = 5) as products_price 
        from products_description pd, products p 
        left join manufacturers m on p.manufacturers_id = m.manufacturers_id 
        left join currencies c on c.code = m.manufacturers_currency 
        left join specials s on p.products_id = s.products_id, products_to_categories p2c 
        where p.products_status = '1' and p.products_id = p2c.products_id and pd.products_id = p2c.products_id and pd.language_id = '1' and p2c.categories_id = '2716'