dsfw2154 2016-04-28 16:12
浏览 115

在MySQL查询中添加字段

I need to add a field in one of our query. I'm nt a PHP programmer buy I can get my way around a little. The query is:

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;
    }

The field that needs to be added is orders_addresses.intProvince so it can be displayed in the results. I tried to understand a little but I guess it's a little more complicated than I thought. It does display the province, which are numbers. My question would be, how do I "translate" those numbers by the actual names so it displays "Ontario" instead of 9? The name of the provinces are in another table called Province.

  • 写回答

1条回答 默认 最新

  • douju1953 2016-04-28 17:35
    关注

    You will need to add another JOIN:

    JOIN Province ON orders_addresses.intprovince = Province.x
    

    And in the SELECT part, replace orders_addresses.intprovince by Province.y

    where

    x = column in table Province that holds the province id
    y = column in table Province that holds the province name

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?