2014-09-13 107 views
0

我試圖列出基於交付國家的訂單。爲此,我在下面寫了SQL查詢。它在數據庫上工作正常。但是當我嘗試在AdminOrderController.php文件上實現時,它會拋出語法錯誤,我嘗試通過更改語法以多種方式解決這個錯誤。mysql where子句語法錯誤prestashop

我該如何實現此條款WHERE country_lang.name ='France' on php-side?

你能幫我解決這個問題嗎?

錯誤消息:

 Bad SQL query 
      You have an error in your SQL syntax; 
      check the manual that corresponds to your MySQL server version 
      for the right syntax to use near 
      'country_lang.name = 'France' ORDER BY a.`id_order` 
      DESC LIMIT 0,50' at line 22 

SQL查詢:

SELECT 
     ps_orders.id_currency, 
     ps_orders.id_order AS id_pdf, 
     CONCAT(LEFT(c.`firstname`, 1), '. ', c.`lastname`) AS `customer`, 
     osl.`name` AS `osname`, 
     os.`color`, 
     IF((SELECT COUNT(so.id_order) FROM `ps_orders` so WHERE so.id_customer = 
      ps_orders.id_customer) > 1, 0, 1) as new, 
     country_lang.name as cname, 
     IF(ps_orders.valid, 1, 0) badge_success 
    FROM ps_orders 

    LEFT JOIN `ps_customer` c ON (c.`id_customer` = ps_orders.`id_customer`) 
    INNER JOIN `ps_address` address ON address.id_address = 
       ps_orders.id_address_delivery 
    INNER JOIN `ps_country` country ON address.id_country = country.id_country 
    INNER JOIN `ps_country_lang` country_lang ON (country.`id_country` = 
       country_lang.`id_country` AND country_lang.`id_lang` = 3) 
    LEFT JOIN `ps_order_state` os ON (os.`id_order_state` = ps_orders.`current_state`) 
    LEFT JOIN `ps_order_state_lang` osl ON (os.`id_order_state` = osl.`id_order_state` 
       AND osl.`id_lang` = 3) 

    WHERE country_lang.name = 'France' 
    ORDER BY id_order DESC 

腓代碼:

$this->_select = ' 
        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(so.id_order) FROM `'._DB_PREFIX_.'orders` so WHERE so.id_customer = a.id_customer) > 1, 0, 1) as new, 
        country_lang.name as cname, 
        IF(a.valid, 1, 0) badge_success'; 

    $this->_join = ' 
        LEFT JOIN `'._DB_PREFIX_.'customer` c ON (c.`id_customer` = a.`id_customer`) 
        INNER JOIN `'._DB_PREFIX_.'address` address ON address.id_address = a.id_address_delivery 
        INNER JOIN `'._DB_PREFIX_.'country` country ON address.id_country = country.id_country 
        INNER JOIN `'._DB_PREFIX_.'country_lang` country_lang ON (country.`id_country` = country_lang.`id_country` AND country_lang.`id_lang` = '.(int)$this->context->language->id.') 
        LEFT JOIN `'._DB_PREFIX_.'order_state` os ON (os.`id_order_state` = a.`current_state`) 
        LEFT JOIN `'._DB_PREFIX_.'order_state_lang` osl ON (os.`id_order_state` = osl.`id_order_state` AND osl.`id_lang` = '.(int)$this->context->language->id.')'; 

    $this->_where = ''; 
    $this->_orderBy = 'id_order'; 
    $this->_orderWay = 'DESC'; 

解決方案:

$this->_where = 'AND country_lang.id_country = 8'; 
+0

您是否試過'$ this - > _ where =「country_lang.name ='France'」;'? – VMai 2014-09-13 13:38:10

+0

是的,但它不起作用。我也試過'country_lang.name ='法國''; – efendi 2014-09-13 13:39:39

+0

你會得到什麼錯誤?兩者都應該工作,請參閱[文檔](http://doc.prestashop.com/display/PS16/Accessing+the+database) – VMai 2014-09-13 13:40:49

回答

0

解決方案:

$this->_where = 'AND country_lang.id_country = 8';