Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql Codeigniter活动记录具有聚合函数MAX的子句_Mysql_Codeigniter_Activerecord_Aggregate Functions - Fatal编程技术网

Mysql Codeigniter活动记录具有聚合函数MAX的子句

Mysql Codeigniter活动记录具有聚合函数MAX的子句,mysql,codeigniter,activerecord,aggregate-functions,Mysql,Codeigniter,Activerecord,Aggregate Functions,我试图在SQL查询中使用聚合函数和HAVING子句,使用Codeigniter的活动记录 function get_last_each() { $this->load->database(); $q = $this->db->from('points')->group_by('nodeid')->having('max(timestamp)', NULL)->get(); return $q->result_array()

我试图在SQL查询中使用聚合函数和HAVING子句,使用Codeigniter的活动记录

function get_last_each()
{
    $this->load->database();
    $q = $this->db->from('points')->group_by('nodeid')->having('max(timestamp)', NULL)->get();

    return $q->result_array();
}
它给了我一个数据库错误:

You have an error in your SQL syntax ...
SELECT * FROM (`points`) GROUP BY `nodeid` HAVING max(timestamp) = 
如果在第二个参数中使用
max(timestamp)
,则生成的查询以
=max(timestamp)
结束。还使用了第三个参数来禁止查询转义,但没有成功

查询在mysql客户端上运行良好:

mysql> select nodeid, timestamp from points group by nodeid having max(timestamp);
+-----------+---------------+
| nodeid    | timestamp     |
+-----------+---------------+
| 10EF7F    | 1400497730790 |
| ADCFB2    | 1400501296786 |
...

使用having函数使用Codeigniter如何实现这一点?

您的having子句没有意义,having是在您执行诸如=、>等操作时使用的。活动记录库(如wise)正在查找要比较的值,如您所见

HAVING max(timestamp) = 
同一个查询可以重新构建为

select nodeid, max(timestamp) from points group by nodeid

谢谢,你说得对。我把实际问题改成了你的建议。对Codeigniter的调用类似:$q=$this->db->select('nodeid,max(timestamp))->from('points')->groupby('nodeid')->get();