Mysql_fetch_array($ result,MYSQL_BOTH)并不准确

时间:2013-06-14 03:37:18

标签: mysql

mysql_fetch_array( $result , MYSQL_BOTH )我遇到了一个奇怪的问题。这就是问题的再现方式:

  1. 执行使用LEFT JOINGROUP BY
  2. 的查询字符串
        select *, 
               (A1.total - A2.borrow_total) as remain_total 
          from ( select Min(sample_created_date) as created_date, 
                 sample_model,
                 sample_ncc, 
                 count(*) as total , 
                 sample_sort 
                  from tb_sample 
                 where 1=1 
                   AND sample_kho IN ( 'hanoi','tm_hanoi' ) 
                 group by sample_model, sample_sort, sample_ncc 
               ) A1 left join 
               ( select sample_model as sample_model1, 
                        count(*) as borrow_total, 
                        sample_ncc 
                   from tb_sample B1 left join tb_sample_ticket B2 
                     on B1.sample_borrow_status = B2.ticket_id 
                  where 1=1 
                    and ticket_bo_phan_duyet = 'finish' 
                    and ticket_status_duyet = '1' 
                    AND sample_kho IN ( 'hanoi','tm_hanoi' ) 
                  group by sample_model, sample_ncc 
                ) A2 on A1.sample_model = A2.sample_model1 
                    AND A1.sample_ncc = A2.sample_ncc left join tb_product_sort A3 
                     on A1.sample_sort = A3.sort_code 
        where 1=1 
        order by created_date DESC 
        LIMIT 0, 30
    
    1. 实施mysql_fetch_array( $result , MYSQL_BOTH )

      $rw  = mysql_fetch_array( $result , MYSQL_BOTH );
      
    2. $rw的显示值:

      echo  $rw['sample_cc'];
      
    3. 但没有什么。然后我尝试print_r( $rw ),见下面的结果:

      Array
      (
          [0] => 2013-06-13 04:10:39
          [created_date] => 2013-06-13 04:10:39
          [1] => 3G
          [sample_model] => 3G
          **[2] => Gmobile
          [sample_ncc] =>** 
          [3] => 1
          [total] => 1
          [4] => SIM
          [sample_sort] => SIM      
      );
      

      我看到 $Rw[2] = 3G ,但 $rw['sample_ncc'] is null

      这是一个奇怪的问题。请帮我解决一下。

1 个答案:

答案 0 :(得分:0)

尝试为子查询sample_nccA1中的A2列提供明确的别名(例如sample_ncc_a1sample_ncc_a2)或仅返回LEFT JOIN的左侧(意为A1)。