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 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看