【问题标题】:how to perfom this query with mysql and php to export excel sheet如何使用 mysql 和 php 执行此查询以导出 excel 表
【发布时间】:2016-10-31 11:02:50
【问题描述】:

您好,我这里有一个问题,我有 2 个表,其中一个名为 ecom_registered_users 和 points。

points 表有名为 points.customer_id 的列,与 ecom_registered_users.id 相同。

在我的 cms 上,我最近有一个查看所有用户信息的页面,我添加了导出按钮以将 excel 表导出为报告,以防客户到目前为止需要它, 问题是我想得到每个客户的总分,我的意思是计算它们的总和,然后当我导出数据时,我应该找到用户名、他的电话号码、他的电子邮件和 + 我的 excel 表中的另一列,其中包含每个用户的总分,我该怎么做

这是我的积分表结构 points table

这是我的 ecom_registered_users

users table

这是我应该运行查询的导出代码块

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    好的,所以我通过使用如下视图来解决我的问题:

    1-查看total_points_plus(所有加号为'+'的点)

    create view total_points_plus as select customer_id , sum(number) as plus from points where (customer_id in (select id from ecom_registered_users) and plus_minus = '+') group by customer_id
    

    2-查看total_points_plus(所有加号为'-'的点)

    create view total_points_minus as select customer_id , sum(number) as minus from points where (customer_id in (select id from ecom_registered_users) and plus_minus = '-') group by customer_id
    

    3-视图对以上视图进行简单计算(总计)

    create VIEW total_points AS  select total_points_plus.customer_id , plus , IFNULL( minus , 0) as minus , (plus - IFNULL(minus , 0)) as grand_total from total_points_plus left join total_points_minus on total_points_plus.customer_id = total_points_minus.customer_id
    

    4- 将最终报告也作为视图获取

    create view final_report as select ecom_registered_users.userName as name  , ecom_registered_users.userEmail as email , ecom_registered_users.userMobile as mobile , total_points.grand_total as total from ecom_registered_users left join total_points on ecom_registered_users.id = total_points.customer_id
    

    就是这样,然后在我的 php 导出代码中,我简单而优雅地执行此查询:

    <?php $query = "SELECT * FROM final_report " ;  ?>
    

    【讨论】:

      【解决方案2】:

      试试这个

      <?php
       session_start();
      
      
       $sql= mysql_query("$qry = "SELECT eru.userName, eru.userMobile, eru.userEmail, SUM(p.number)
          FROM ecom_registered_users eru 
          JOIN points p ON eru.customer_id = p.id
          WHERE eru.customer_id = /*insert customer id here*/;"") or die(print "Gagal Download :".mysql_error());
      $columns_total = mysql_num_fields($sql);
      // Get The Field Name
      
      for ($i = 0; $i < $columns_total; $i++) {
      $heading = mysql_field_name($sql, $i);
      $output .= '"'.$heading.'",';
      }
      $output .="\n";
      
      // Get Records from the table
      
      while ($row = mysql_fetch_array($sql)) {
      for ($i = 0; $i < $columns_total; $i++) {
      $output .='"'.$row["$i"].'",';
      }
      $output .="\n";
      }
      
      // Download the file
      
      $filename = "filename.csv"; //Ur File name
      header('Content-type: application/csv');
      header('Content-Disposition: attachment; filename='.$filename);
      
      echo $output;    
      exit;
      ?>
      

      【讨论】:

      • 好吧,似乎只对一个用户有好处,如果我想为所有用户做这件事,而不仅仅是一个,这意味着我不会指定一个 id,怎么做
      • 只删除 > WHERE eru.customer_id = /*insert customer id here*/
      【解决方案3】:

      很遗憾,我看不到您的代码块,所以我不确定您已经尝试过什么,但您似乎只需要这样的声明:

      $qry = "SELECT eru.userName, eru.userMobile, eru.userEmail, SUM(p.number)
              FROM ecom_registered_users eru 
              JOIN points p ON eru.customer_id = p.id
              WHERE eru.customer_id = /*insert customer id here*/;";
      

      此查询应该为您提供一位用户想要的结果。如果您需要一页上的所有用户,您只需将 GROUP BY 和 ORDER 添加到查询中。如果您向我展示您的代码,我可以编辑此答案以帮助您更多。

      【讨论】:

      • 好的,但你知道如何将其作为两个表之间的视图,我将从视图中选择我想要的任何内容
      • 如果您想将所有信息下载到 CSV 文件中,然后编辑掉以后不需要的内容,可以使用以下查询:SELECT eru.userName, eru.userMobile, eru.userEmail, SUM(p.number) FROM ecom_registered_users eru JOIN points p ON eru.customer_id = p.id GROUP BY eru.customer_id ORDER BY SUM(p.number)
      猜你喜欢
      • 2014-02-17
      • 1970-01-01
      • 2015-03-23
      • 2011-08-09
      • 1970-01-01
      • 1970-01-01
      • 2021-10-30
      相关资源
      最近更新 更多