2017-07-18 107 views
-1

我一直在試圖修復這個PHP MySQL查詢整個下午,我迷失了主意!以下查詢工作!:PHP MySQL查詢語句問題

$sql = "SELECT o.order_id, CONCAT(o.firstname, ' ', o.lastname) AS customer, o.shipping_code, o.total, o.channel, o.currency_code, o.currency_value, o.date_added, o.date_modified, c.image, 
     cg.name AS cgroup, CONCAT(o.payment_address_1, '<br>', o.payment_city, ', ', o.payment_zone, ' ', o.payment_postcode) AS address, o.telephone AS phone, o.email AS email,    
     (SELECT os.name FROM " . DB_PREFIX . "order_status os WHERE os.order_status_id = o.order_status_id AND os.language_id = '" . (int)$this->config->get('config_language_id') . 
     "') AS order_status FROM `" . DB_PREFIX . "order` o LEFT JOIN `" . DB_PREFIX . "customer` c ON(o.customer_id=c.customer_id) LEFT JOIN `" . DB_PREFIX . "customer_group_description` 
     cg ON(c.customer_group_id=cg.customer_group_id)"; 

但是,當我嘗試添加以下它打破。

(SELECT action FROM " . DB_PREFIX . "donate_history h LEFT JOIN " . DB_prefix . "order od ON (h.customer_id = od.customer_id)), 

我不要如果我寫的權利。我重寫了它,刪除了部分內容,試圖儘可能簡單,但我無法修復它。這是我一直在嘗試的:

$sql = "SELECT o.order_id, CONCAT(o.firstname, ' ', o.lastname) AS customer, o.shipping_code, o.total, o.channel, o.currency_code, o.currency_value, o.date_added, o.date_modified, c.image, 
     cg.name AS cgroup, CONCAT(o.payment_address_1, '<br>', o.payment_city, ', ', o.payment_zone, ' ', o.payment_postcode) AS address, o.telephone AS phone, o.email AS email, 
     (SELECT action FROM " . DB_PREFIX . "donate_history h LEFT JOIN " . DB_prefix . "order od ON (h.customer_id = od.customer_id)),    
     (SELECT os.name FROM " . DB_PREFIX . "order_status os WHERE os.order_status_id = o.order_status_id AND os.language_id = '" . (int)$this->config->get('config_language_id') . 
     "') AS order_status FROM `" . DB_PREFIX . "order` o LEFT JOIN `" . DB_PREFIX . "customer` c ON(o.customer_id=c.customer_id) LEFT JOIN `" . DB_PREFIX . "customer_group_description` 
     cg ON(c.customer_group_id=cg.customer_group_id)"; 
+1

什麼是錯誤? – Ali

+0

您沒有該列的任何別名。在關閉的圓括號內寫上「as action」之類的東西,看其他人看看我的意思。 – Bryan

+1

兩個意見:調用一個表「訂單」是在尋求麻煩;如果有payment_address_2怎麼辦? – Strawberry

回答

1

看起來像你嘗試添加的新子查詢沒有別名。

SELECT o.order_id, CONCAT(o.firstname, ' ', o.lastname) AS customer, o.shipping_code, o.total, o.channel, o.currency_code, o.currency_value, 
    o.date_added, o.date_modified, c.image, cg.name AS cgroup, CONCAT(o.payment_address_1, '<br>', o.payment_city, ', ', o.payment_zone, ' ', o.payment_postcode) AS address, 
    o.telephone AS phone, o.email AS email, 
    (SELECT action 
     FROM " . DB_PREFIX . "donate_history h 
     LEFT JOIN " . DB_prefix . "order od 
      ON (h.customer_id = od.customer_id)) AS donate_history,    
    (SELECT os.name 
     FROM " . DB_PREFIX . "order_status os 
      WHERE os.order_status_id = o.order_status_id AND os.language_id = '" . (int)$this->config->get('config_language_id') . 
    "') AS order_status 
    FROM `" . DB_PREFIX . "order` o 
     LEFT JOIN `" . DB_PREFIX . "customer` c 
      ON(o.customer_id=c.customer_id) 
     LEFT JOIN `" . DB_PREFIX . "customer_group_description` cg 
      ON(c.customer_group_id=cg.customer_group_id)