2016-04-02 207 views
0

我想从我的数据库表中获取这样的报告。通过php生成报告

report

事情做报告时,应该考虑是:

  1. 报告将基于所选择的日期。
  2. 两台数据库和两个表(已加入)“公告”和“客户”

  3. 展厅有showroom_name每个都在客户
    表相关的记录。一个展厅可能每天都有几个客户询问。

  4. 在一天中可能会有来自每个展厅的查询数量。

  5. 假设一个月的第一天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 

查询结果

query result

,我被打上两个数据有两个不同的日期。 2016-02-09和2016-02-23。 '356'是showroom_id,那么报告的第9列成为1,第23天的列成为1.希望你明白我的观点。我如何计算每个展厅的查询数量?

赫斯的代码片段,我至今写。不知道你是否需要更多详细信息,请在下面评论写事后

<?php 

echo $row->showroom_name; 

$conn = mysql_connect('localhost', 'root', '[email protected]') 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_niccu_agecu_occcu_landpurchase_typepref_modelpref_coloroth_modelpurchase_datecurrenrt_bikebike_year,att_by,cu_made, about_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

谢谢。

+0

如果提供你的问题的SQL拨弄它是我们更容易尝试的东西: http://sqlfiddle.com/ – Jester

+0

您的订单添加日期BY子句。其余的将在PHP中完成。 – Strawberry

回答

0

我想这应该做的伎俩:

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,可能不是neccesary