简体   繁体   English

根据带有 unix 时间戳的月份和年份选择总和

[英]select sum based on month and year with a unix timestamp

I am trying to sum all the values in a column for month 10 which is october, but i seem to have unix timestamp我正在尝试对 10 月的第 10 个月的列中的所有值求和,但我似乎有 unix 时间戳

i tried the following but the value remains 0我尝试了以下但值仍然为 0

<?php 
echo $this->db->select('(SELECT SUM(amount_paid) FROM invoice WHERE MONTH (  `creation_timestamp` ) = 10) AS invoice');

$query = $this->db->get('invoice'); 
?>

.. ..

Schema架构

CREATE TABLE `invoice`(
    `invoice_id` int(11) NOT NULL AUTO_INCREMENT,
    `student_id` int(11) NOT NULL,
    `title` longtext COLLATE utf8_unicode_ci NOT NULL,
    `description` longtext COLLATE utf8_unicode_ci NOT NULL,
    `amount` int(11) NOT NULL,
    `amount_paid` longtext COLLATE utf8_unicode_ci NOT NULL,
    `due` longtext COLLATE utf8_unicode_ci NOT NULL,
    `creation_timestamp` int(11) NOT NULL,
    `payment_timestamp` longtext COLLATE utf8_unicode_ci NOT NULL,
     `payment_method` longtext COLLATE utf8_unicode_ci NOT NULL,
    `payment_details` longtext COLLATE utf8_unicode_ci NOT NULL,
    `status` longtext COLLATE utf8_unicode_ci NOT NULL COMMENT 'paid or unpaid',
    PRIMARY KEY (`invoice_id`)
) ENGINE=MyISAM AUTO_INCREMENT=73 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

give this a shot as we are looking for the sum for October 2015 (I think)试一试,因为我们正在寻找 2015 年 10 月的总和(我认为)

SELECT UNIX_TIMESTAMP('2015-10-01'); -- '1443672000'
SELECT UNIX_TIMESTAMP('2015-10-31 23:59:59'); -- 1446350399
SELECT UNIX_TIMESTAMP('2015-11-01'); -- '1446350400'

Note the above values注意上面的值

drop table invoice2;
create table invoice2
(   id int auto_increment primary key,
    amount decimal(12,2) not null,
    ts int(11) not null -- from unix timestamp
);

-- truncate table invoice2;
insert invoice2(amount,ts) values 
(10,unix_timestamp('2015-10-01')),
(10,unix_timestamp('2015-10-01')),
(3310,unix_timestamp('2015-11-01')),
(3310,unix_timestamp('2015-11-01')),
(44410,unix_timestamp('2016-01-01')),
(5510,unix_timestamp('2016-02-01')),
(6610,unix_timestamp('2016-02-01'));

So you want something in the following flavor for October 2015 amounts:因此,对于 2015 年 10 月的金额,您需要以下风格的东西:

select sum(amount) as theSum 
from invoice2 
where month(from_unixtime(ts))=10  
and year(from_unixtime(ts))=2015;
+--------+
| theSum |
+--------+
|  20.00 |
+--------+

the function of interest is get_sum()感兴趣的函数是get_sum()

Codeigniter Model Codeigniter 模型

 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 class department_model extends CI_Model{

 function __construct()
 {
      // Call the Model constructor
      parent::__construct();
 }

 //read the department list from db
 function get_department_list()
 {
      $sql = 'select var_dept_name, var_emp_name from tbl_dept, tbl_emp where tbl_dept.int_hod = tbl_emp.int_id';
      $query = $this->db->query($sql);
      $result = $query->result();
      return $result;
 }

 // get the sum of amount based on parameters month and year passed
 function get_sum($month,$year) 
 {    $myQuery="select sum(amount) as theSum from invoice2 where month(from_unixtime(ts))=$month  
                and year(from_unixtime(ts))=$year";
      $query = $this->db->query($myQuery);

      $result = $query->result();
      return $result;
 }

} }

There is some problems in your query and code as well.您的查询代码中也存在一些问题。 when you execute your query directly on database it is giving error.当您直接在数据库上执行查询时,它会出错。 Also for custom queries we have to use $this->db->query() function in CI.同样对于自定义查询,我们必须在 CI 中使用$this->db->query()函数。 Below is the correct query code snippet.下面是正确的查询代码片段。

<?php 
   echo $this->db
             ->query('SELECT SUM(amount_paid) AS invoice FROM invoice WHERE MONTH (  `creation_timestamp` ) = 10 ')
             ->row()->invoice;
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM