如何在codeigniter sql查询中编写TIMESTAMPDIFF?

时间:2011-02-18 20:58:38

标签: php mysql codeigniter timestamp

在phpmyadmin中我可以运行此查询:

SELECT TIMESTAMPDIFF(SECOND,first_load,last_load) AS uptime FROM table

但是如何以codeignitor方式编写该查询?这不起作用:

$this->db->select('TIMESTAMPDIFF(SECOND,first_load,last_load) AS uptime')

真正的查询有点复杂,所以我只展示了一个部分。这将是完整的查询(不工作):

$this->db
 ->select('n.unique_id, n.name, b.impressions, b.clicks, TIMESTAMPDIFF(SECOND,first_load,last_load) AS uptime')
 ->from(self::$_table . ' AS b')
 ->join('sm_names AS n', 'n.id = b.name_id AND n.blocked = \'0\' AND n.unique_id != 11111111')
 ->where('b.last_load >', date('Y-m-d H:i:s', time() - (86400 * 2)))
 ->order_by($order, 'desc')
 ->limit($limit);

它会生成以下错误消息:http://screencast.com/t/ZUYq58Rc6uA

感谢。

1 个答案:

答案 0 :(得分:2)

在括号关闭之前尝试在TIMESTAMPDIFF(SECOND,first_load,last_load)中的last_load之后给出一个空格。