2016-04-02 113 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