2016-04-28 51 views
0

我需要在我們的查詢中添加一個字段。我只是一個PHP程序員購買,我可以繞過一點點。查詢是:在MySQL查詢中添加一個字段

if (_QUERYSTRING_) { 
    switch ($intMode) { 
     case -1: 
      $result = mysqli_query($mysql," 
      SELECT orders.id, 
        orders_addresses.strlastname, 
        orders_addresses.strfirstname, 
        orders_addresses.intprovince, 
        9 AS intmode, 
        Date(orders.dtimeorder) AS datepayment, 
        orders_costs.dbltotal AS dblamount, 
        orders_notes.strcod AS strtxn, 
        0 AS dblfee, 
        shipping_postalservices.strtracking"._LANG_." as strtrackingurl, 
        'À recevoir' AS strmode, 
        NULL AS strvendor 
      FROM  orders 
      JOIN  orders_costs 
      ON  orders_costs.id = orders.id 
      JOIN  orders_addresses 
      ON  orders_addresses.id = orders.id 
      JOIN  orders_notes 
      ON  orders_notes.id = orders.id 
      JOIN  shipping_postalservices 
      ON  shipping_postalservices.id = orders_costs.intpostalservice 
      WHERE date(orders.dtimeorder) BETWEEN '".date("Y-m-d",$timeStart)."' AND  '".date("Y-m-d",$timeEnd)."' 
      AND  orders.boolshipped = 1 
      AND  orders.boolcanceled = 0 
      AND  orders_costs.boolcod = 1 
      AND  orders_costs.dbltotal > 0 
      AND  NOT EXISTS 
        ( 
         SELECT * 
         FROM orders_payments 
         WHERE orders_payments.intorder = orders.id 
         AND orders_payments.intmode = 9 
         AND orders_payments.dblamount > 0) 
     GROUP BY orders.id 
     ORDER BY orders.dtimeorder, 
       orders.id"); 
      break; 
     default: 
      $result = mysqli_query($mysql," 
      SELECT orders.id, 
         orders_addresses.strlastname, 
         orders_addresses.strfirstname, 
         orders_addresses.intprovince, 
         orders_payments.intmode, 
         Date(orders_payments.dtimepayment) AS datepayment, 
         orders_payments.dblamount, 
         orders_payments.strtxn, 
         orders_payments.dblfee, 
         shipping_postalservices.strtracking"._LANG_." as strtrackingurl, 
         payments.strname"._LANG_." AS strmode, 
         payments.strvendor 
      FROM  orders_payments 
      JOIN  orders 
      ON  orders.id = orders_payments.intorder 
      JOIN  orders_costs 
      ON  orders_costs.id = orders.id 
      JOIN  orders_addresses 
      ON  orders_addresses.id = orders.id 
      JOIN  shipping_postalservices 
      ON  shipping_postalservices.id = orders_costs.intpostalservice 
      LEFT JOIN payments 
      ON  payments.id = orders_payments.intmode 
      WHERE  date(orders_payments.dtimepayment) BETWEEN '".date("Y-m-d",$timeStart)."' AND  '".date("Y-m-d",$timeEnd)."'".(!empty($intMode) ? " 
      AND  orders_payments.intmode = '".$intMode."'" : NULL)." 
      GROUP BY orders.id, 
         orders_payments.intpayment 
      ORDER BY orders_payments.dtimepayment, 
         orders.id"); 
      break; 
    } 

需要添加是orders_addresses.intProvince因此它可以顯示在結果的字段。我試圖理解一點,但我想這比我想象的要複雜一點。它確實顯示該省,這是數字。我的問題是,如何將實際名稱「翻譯」這些數字,以顯示「Ontario」而不是9?各省的名稱在另一個名爲Province的表中。

+0

因此... w帽子的問題? – Mike

+0

同意@Mike - 只需將它添加(,orders_addresses.intProvince as Something) - 在當前SELECT列表的末尾和FROM之前。 – user3741598

+0

嘗試格式化您的查詢,而不是使用單一行1000字符長 – Mike

回答

0

您將需要添加另一個JOIN:

JOIN Province ON orders_addresses.intprovince = Province.x 

而在SELECT部分​​,由Province.y

其中

X =表省列舉行全省ID替換orders_addresses.intprovince
y =表省內的省名持有省名