지난 한 달 동안 사용자가 특정 연도의 매주 데이터를 보유하는 범주 & 벤치 마크 항목을 추가 할 수있는 벤치 마크 시스템을 만들었습니다. 그 주에 대한 데이터가 없거나 1 년 동안 데이터가 없더라도 연중 모든 주 동안 결과가있는 각 benchmarkitem을 표시하는 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 문입니다. 올해의 함수 param에 기본값을 설정하려고합니다.
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