在過去的一個月裏,我一直在創建一個基準測試系統,允許用戶添加類別&基準測試項目,該測試項目在特定年份內每週保留數據。我無法創建一個mysql語句,該語句在每年的所有星期內顯示每個基準項目的結果,即使那一週沒有數據或一年中沒有數據。以下是我一直在測試的查詢和表格列。我正在使用codeigniter,並計劃以底部顯示的格式顯示結果。我爲這篇冗長的描述道歉,但我試圖儘可能清楚。任何幫助將不勝感激。MYSQL-在循環周結束日期時顯示數據
tblbenchmarkitem
=================
itemID | itemDescription | itemTarget | ItemFreq | FKcategoryID
tblbenchmarkData
================
dataID | FKitemID | resultDate | result | dateAdded | dateModified
tblcategories
===============
categoryID | categoryName | parentID | FKdeptID
tblcalendardates - stores 52 weeks for year Saturday to Friday
================
id | year | startDate | endDate
這些是我在模型中的mysql語句。我將在該年的函數參數中設置一個默認值。
public function get_data() {
$dates = $this->db->query("SELECT endDate FROM tblcalendardates WHERE year = '2014' ORDER BY endDate");
$dates = $dates->result();
foreach ($dates as $date) {
//$query = "SELECT tblcalendardates.enddate, tblbenchmarkitems.itemDescription, tblbenchmarkitems.itemTarget, tblbenchmarkitems.itemFrequency, tblbenchmarkdata.resultDate, IFNULL(tblbenchmarkdata.result,0) AS result, (SELECT strCategoryName FROM tblcategories WHERE tblbenchmarkitems.FKcategoryID = tblcategories.categoryID) AS category FROM tblbenchmarkdata LEFT JOIN tblbenchmarkitems ON tblbenchmarkdata.FKitemID = tblbenchmarkitems.itemID JOIN tblcalendardates WHERE DATE(tblbenchmarkdata.resultDate) BETWEEN (SELECT MIN(DATE(tblcalendardates.startdate)) FROM tblcalendardates WHERE tblcalendardates.year = " . date('Y') . ") AND (SELECT MAX(DATE(tblcalendardates.enddate)) FROM tblcalendardates WHERE tblcalendardates.year = " . date('Y') . ") GROUP BY category ORDER BY tblcalendardates.enddate";
$query = 'SELECT c.*, i.itemDescription, i.itemTarget, IFNULL(d.result, 0) as result,"' . $date ->endDate . '" FROM tblcategories AS c JOIN tblbenchmarkitems AS i ON i.FKcategoryID = c.categoryID JOIN tblbenchmarkdata as d ON i.itemID = d.FKitemID JOIN tblcalendardates WHERE DATE(d.resultDate) BETWEEN (SELECT MIN(DATE(tblcalendardates.startdate)) FROM tblcalendardates WHERE tblcalendardates.year = "' . date('Y') . '") AND (SELECT MAX(DATE(tblcalendardates.enddate)) FROM tblcalendardates WHERE tblcalendardates.year = "' . date('Y') . '") GROUP BY strCategoryName';
$query = $this->db->query($query);
return $query->result();
}
}
這在我看來是
<table class="table table-hover data-list">
<thead>
<tr>
<th class="th-set-width1">Benchmark Item</th>
<th class="th-set-width2">Target</th>
<?php foreach($weeks as $week): ?>
<th class="th-set-width2"><?php echo date('m-d-Y', strtotime($week->enddate)); ?></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?php foreach($entries as $entry) : ?>
<tr>
<td><?php echo $entry->itemDescription; ?></td>
<td><?php echo $entry->itemTarget; ?></td>
<?php for ($i = 0; $i < 52; $i++) : ?>
<?php if ($entry->result != NULL || $entry->result > 0): ?>
<td><?php echo $entry->result; ?></td>
<?php else: ?>
<td>0</td>
<?php endif; ?>
<?php endfor; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
這是我想在一個表格中顯示的數據。
-----------------------------------------------------------------------------------
categoryName
====================================================================================
itemDescription | itemTarget | 01-03-14 | 01-10-14 | 01-17-14 | 01-24-14 | 01-31-14
=====================================================================================
No. of Visits | 12.00 | NULL | NULL | 15.00 | NULL | 20.00
No. of Calls | 17.00 | 12.00 | NULL | 17.00 | 22.00 | NULL