我有 2 张桌子:
事件 (id, event_description) 评论 (id, event_id, comment_description)
我想写这样的 SQL 表达式:
SELECT incident.*, COUNT(comment.id) AS com
FROM incident
LEFT JOIN comment ON comment.incident_id=incident.id
GROUP BY incident.id
ORDER BY com DESC
它在 phpmyadmin 中运行良好。
我用 ORM 写:
ORM::factory('incident')
->select('incident.*',array('COUNT("comment.id")', 'com'))
->join('comment', 'LEFT')
->on('comment.incident_id', '=', 'incident.id')
->group_by('incident.id')
->order_by('com', 'DESC')
->find_all();
但我收到一个错误: system/libraries/Database.php [296]: * trim() 期望参数 1 是字符串,给定数组*
来自 Database.php 的代码:
foreach ($sql as $val)
{
if (($val = trim($val)) === '') continue;
if (strpos($val, '(') === FALSE AND $val !== '*')
{
if (preg_match('/^DISTINCT\s++(.+)$/i', $val, $matches))
{
$val = $this->config['table_prefix'].$matches[1];
$this->distinct = TRUE;
}
else
{
$val = (strpos($val, '.') !== FALSE) ? $this->config['table_prefix'].$val : $val;
}
$val = $this->driver->escape_column($val);
}
$this->select[] = $val;
}