通过php生成报告

uurv41yg  于 2023-04-28  发布在  PHP
关注(0)|答案(1)|浏览(145)

我想从我的数据库表中得到这样的报告。

做报告时应该考虑的事项有:
1.报告将根据所选日期生成。
1.两个数据库和两个表(已连接)'showroom'和'customer'

  1. showroom有showroom_name,每个showroom在customer中有相关记录
    一个展厅每天可能有几个客户咨询。
    1.在一天内可能会有来自每个展厅的询问数量。
    1.比如说,如果一个月的第一天5查询,它应该出现在日期栏下和前面的具体陈列室名称。
    我从两个表中选择数据的查询工作正常。
SELECT
    abans_crm.customers.added_date,
    abans_crm.customers.location_id
    FROM
    abans_crm.customers
    INNER JOIN sh_sso.showrooms ON sh_sso.showrooms.showroom_id = abans_crm.customers.location_id
    WHERE
    abans_crm.customers.added_location = -99 AND
    abans_crm.customers.type_id = 1
    ORDER BY
    abans_crm.customers.location_id ASC

查询结果为

其中我被标记为两个数据有两个不同的日期。2016-02-09和2016-02-23。'356'是showroom_id,因此报告的第9天列变为1,第23天列变为1。希望你明白我意思。我如何计算每个展厅的询价数量?

这是我写的代码片段。我不知道以后写什么

<?php 
                              
echo $row->showroom_name;
  
$conn = mysql_connect('localhost', 'root', 'Sate11it@') or die("Unable to connect to MySQL");
  
mysql_select_db('sh_sso', $conn); 
mysql_select_db('abans_crm', $conn);
  
$query_3 = "SELECT
                abans_crm.customers.added_date,
                abans_crm.customers.location_id
                FROM
                abans_crm.customers
                INNER JOIN sh_sso.showrooms 
                ON sh_sso.showrooms.showroom_id = abans_crm.customers.location_id
                WHERE
                abans_crm.customers.added_location = -99 AND
                abans_crm.customers.type_id = 1
                ORDER BY
                abans_crm.customers.location_id ASC";
                  
$retval = mysql_query($query_3);
                                                    
if(!$retval ) {
   die('Could not get data: ' . mysql_error());
}

$select_month = '2';                                   
$select_year = '2016'; 

$days = cal_days_in_month(CAL_GREGORIAN,$select_month,$select_year);

while ($row2 = mysql_fetch_assoc($retval)){
                        
    $year = date('Y',strtotime($row2['added_date']));
    $month = date('m',strtotime($row2['added_date']));
    $date = date('m',strtotime($row2['added_date']));
                        
    $showroom_id = $row2['location_id'];
                       
    if($year == $select_year && $month == $select_month){
      
        for($a = 1; $a <= $days; $a++){
           echo "<td id=".$a.">".$count."</td>"; 
        }

    }
 }
?>

如果你需要更多的细节,请在下面评论。

客户表字段cu_idcu_namecu_emailcu_addresscu_phonecu_remarkadded_datelast_updateadded_bystatus_idlocation_idcu_namecu_agecu_occcu_landpurchase_typepref_modelpref_coloroth_modelpurchase_datecurrenrt_bikebike_yearatt_bycu_madeabout_herosource_idadded_ipdealer_flagadded_locationcur_milagecat_idtype_id
展厅表格字段showroom_idshowroom_codeshowroom_nameshowroom_addressaddress_citymanager_namemanager_mobilemanager_idshop_emailshop_phoneshop_faxshowroom_typeadded_datelast_updateadded_bystatus_idlevel_id

谢谢大家。

odopli94

odopli941#

我想这个应该可以了:

SELECT DATE(abans_crm.customers.added_date) AS new_added_date
       COUNT(DATE(abans_crm.customers.added_date)) AS customer_amount,
       abans_crm.customers.location_id
FROM abans_crm.customers
INNER JOIN sh_sso.showrooms ON sh_sso.showrooms.showroom_id = abans_crm.customers.location_id
WHERE abans_crm.customers.added_location = -99 
AND abans_crm.customers.type_id = 1
GROUP BY abans_crm.customers.location_id, new_added_date
ORDER BY abans_crm.customers.location_id ASC, new_added_date ASC

我们使用DATE只获取年、月、日部分。然后,我们将GROUP BYlocation_id以及new_added_date进行分组,这样,只有在年、月和日相同的情况下,结果才会分组。我还在new_added_date上添加了一个ORDER BY,可能不需要

相关问题