在表格中,我有一个日期和金额列,具有不同的日期和不同的金额。所以,其实我想把我的数据显示为
Select Year
Month Amount
Total
但我得到了所有年份数据和每月总数
这是我的模特
public function overviews(){
$sql = "SELECT DATEPART(Year, TRANS_TransactionDate) [TheYear], DATEPART(Month, TRANS_TransactionDate) [TheMonth], DATENAME(Month, TRANS_TransactionDate) [TheMonthName], SUM(Trans_Amount) [TotalAmount]
FROM BTBL_Transactions
GROUP BY DATEPART(Year, TRANS_TransactionDate), DATEPART(Month, TRANS_TransactionDate), DATENAME(Month, TRANS_TransactionDate)
ORDER BY [TheYear], [TheMonth], [TheMonthName]";
$query = $this->db->query($sql);
//echo $this->db->last_query(); die();
return $query->result();
}
这是我的控制器
public function overviews(){
// $merchantid=$this->session->userdata('user_password');
$result=$this->livemerchant_model->overviews();
$name=$this->session->userdata('name');
$data=array('result'=>$result,'name'=>$name);
if($this->session->has_userdata('user_email','name')){
$this->load->view('overviews.php', $data);
}
}
这是我的vie文件
<?php
foreach ($baji as $row) {?>
<tr>
<td><?php echo $row->TheMonthName . PHP_EOL; ?></td>
<td><?php echo $row->TotalAmount. PHP_EOL;?></td>
</tr>
<?php }?>
答案 0 :(得分:0)
如果要按年份选择,则需要在sql中包含WHERE
语句。
SELECT...
WHERE YEAR = user_year_input_here
GROUP BY...