简体   繁体   中英

Group by date mysql and codeigniter

I have table like this:

date_time               value
2015-06-20 10:00:00     10
2015-06-20 9:00:00      6
2015-06-21 6:00:00      5
2015-06-22 11:00:00     12
2015-06-22 10:00:00     11
2015-06-23 4:00:00      20

I want to get the total value of each date Currently I have this on CodeIgniter but is not working:

$this->db->group_by('date_time');
$query = $this->db->get('table_a');

Any idea to fix. Thanks.

You need to ditch the star '*' and specify the expressions you want to return. To get a total, you need an aggregate function, and to get just the date portion of date_time , you can use the DATE() function.

   $this->db->select('DATE(a.date_time) AS date, SUM(a.value) AS total_value');

And you want to GROUP BY DATE(a.date_time) .

   $this->db->group_by('DATE(a.date_time)');
$result = $query->result();

尝试添加这一行。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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