MySQL statements for this date last year
我对这些php mysql语句感到困惑。
我需要从表中的相同列(数量)中获得四个不同的结果。
函数找到以下的(金额)总和:
今年迄今为止
去年给了时间
去年总计给予
在过去30天内给予
它们似乎都是相同的变体。 我不知道如何让LastYearNow显示正确的结果。 如何显示从2016年初到今天2016年的结果?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public function getSum() { $query = $this->db->query("SELECT SUM(amount) as amount_sum FROM" . DB_PREFIX ."donate Where date_added >= (CURDATE() - INTERVAL 12 MONTH)"); return $query->row; } public function lastThirty() { $query = $this->db->query("SELECT SUM(amount) as amount_sum FROM" . DB_PREFIX ."donate Where date_added >= (CURDATE() - INTERVAL 1 MONTH)" ); return $query->row; } public function lastYearNow() { $query = $this->db->query("SELECT SUM(amount) as amount_sum FROM" . DB_PREFIX ."donate Where date_added >= (CURDATE() - INTERVAL 12 MONTH)"); return $query->row; } public function lastYearTotal() { $query = $this->db->query("SELECT SUM(amount) as amount_sum FROM" . DB_PREFIX ."donate Where YEAR(date_added) = YEAR(DATE_SUB(CURDATE(), INTERVAL 1 YEAR))" ); return $query->row; } |
1 2 3 4 5 6 | SELECT SUM(amount) as amount_sum FROM donate WHERE date_added BETWEEN MAKEDATE(YEAR(DATE_SUB(CURDATE(), INTERVAL 1 YEAR)), 1, 1) AND (CURDATE() - INTERVAL 12 MONTH) |
警告:我没有在我面前安装MySQL,因此语法可能不太正确。